Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift TableviewCell中的Collectionview,重复数据_Swift_Tableview_Collectionview_Reuseidentifier_Prepareforreuse - Fatal编程技术网

Swift TableviewCell中的Collectionview,重复数据

Swift TableviewCell中的Collectionview,重复数据,swift,tableview,collectionview,reuseidentifier,prepareforreuse,Swift,Tableview,Collectionview,Reuseidentifier,Prepareforreuse,在可调整大小的tablviewCells中有一个collectionview。Tableview在每个“n”个分区中有一个单元格。collectionview的数据源和委托设置为tableviewCell。tablview的cellForRowAt上调用了一个API,结果在collectionview上为每个单元格呈现。获取结果后,委托会告诉tableview已加载collectionview,并且这次应该在不调用API的情况下重新加载该单元格。但问题是,我的collectionview数据在每

在可调整大小的tablviewCells中有一个collectionview。Tableview在每个“n”个分区中有一个单元格。collectionview的数据源和委托设置为tableviewCell。tablview的cellForRowAt上调用了一个API,结果在collectionview上为每个单元格呈现。获取结果后,委托会告诉tableview已加载collectionview,并且这次应该在不调用API的情况下重新加载该单元格。但问题是,我的collectionview数据在每2个tableviewCells之后重复

我知道prepareForReuse应该被重写以消除单元重用问题。我已经在collectionviewCells中实现了prepareForReuse,并将label.text和imageView.image设置为nil。但是,我不确定要为我的tableviewCell添加什么

//TableView类

override func viewDidLoad(){
super.viewDidLoad()
storiesSections=[…]
register(UINib(nibName:“RWFeedTableViewCell”,bundle:nil),forCellReuseIdentifier:“reuseIdentifier”)
tableView.EstimateDrowEight=1
tableView.rowHeight=UITableView.automaticDimension
}
重写func numberOfSections(在tableView:UITableView中)->Int{
返回storiesSections.count
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
var cell=tableView.dequeueReusableCell(标识符为:“reuseIdentifier”,用于:indexPath)作为!RWFeedTableViewCell
如果单元格==nil{
cell=UITableViewCell(样式:UITableViewCell.CellStyle.default,reuseIdentifier:“reuseIdentifier”)作为!RWFeedTableViewCell
}
cell.delegate=self
cell.fetchData(feedSection:storiesSections[indexPath.section],indexPath:indexPath)
返回单元
}
//用于重新加载tableview的委托
func CollectionViewDiEnd(更新的FeedSection:FeedSection,indexPath:indexPath){
storiesSections[indexPath.section]=updatedFeedSection
tableView.beginUpdate()
tableView.endUpdates()
}
//TableViewCell类


重写func awakeFromNib(){
super.awakeFromNib()
初始化代码()
}
func initializeCode(){
//集合布局
self.collectionView.collectionViewLayout=rwWaterAllLayout2()
self.collectionView.register(UINib(nibName:\(imageThentTitleViewCell.self)”,bundle:nil,强制使用reuseIdentifier:kimageThentTitleCellid)
self.collectionView.register(UINib(nibName:\(LeftImageCell.self)”,bundle:nil,forCellWithReuseIdentifier:kLeftImageCellID)
self.collectionView.contentInset=UIedgeInSet(顶部:8,左侧:8,底部:8,右侧:8)
self.collectionView.IsScrolEnabled=false
self.collectionView.dataSource=self
self.collectionView.delegate=self
}
func fetchData(feedSection:feedSection,indexPath:indexPath){
如果feedSection.isLoadComplete{
回来
}
如果让catID=feedSection.categoryID{
设urlString=URL(字符串:)
让urlRequest=urlRequest(url:urlString!)
让config=URLSessionConfiguration.default
let session=URLSession(配置:config)
让task=session.dataTask(带:urlRequest){(数据、响应、错误)在
如果错误==nil{
}否则{
打印(错误?.localizedDescription,如有)
}
guard let responseData=其他数据{
打印(“错误:未收到数据”)
回来
}
做{
guard let todo=将JSONSerialization.jsonObject(带有:responseData,选项:[])作为?[String:Any]其他内容尝试{
打印(“尝试将数据转换为JSON时出错”)
回来
}
打印(“成功将数据转换为JSON”)
DispatchQueue.main.async{
var updatedFeedSection=feedSection
updatedFeedSection.storiesArray?=(todo[“数据”]!as!数组)
updatedFeedSection.isLoadComplete=true
self.feedSection=更新的feedSection
self.collectionView.reloadData()
self.collectionView.performbatchUpdate({
},完成:{(完成)在
self.collectionViewHeightConstraint.constant=self.collectionView.collectionViewLayout.collectionViewContentSize.height+self.collectionView.contentInset.top+self.collectionView.contentInset.bottom
self.delegate?.collectionViewDiEnd(updatedFeedSection:updatedFeedSection,indexPath:indexPath)
})
}
}抓住{
打印(“尝试将数据转换为JSON时出错”)
回来
}
}
task.resume()
}
}
func numberOfSections(在collectionView:UICollectionView中)->Int{
返回1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
如果self.feedSection==nil{
返回0
}否则{
返回(self.feedSection?.storiesArray?.count)!
}
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让indexForTen=indexPath.item%10
让故事=self.feedSection?.storiesArray?[indexPath.item]
如果indexForTen==0{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:kimagestentitlecellid,for:indexPath)作为!imagesh
func fetchData(feedSection: FeedSection, indexPath: IndexPath)  {

        self.feedSection = feedSection

        if self.feedSection.isLoadComplete {
            self.collectionView.reloadData()
            return
        }

        if let catID = feedSection.categoryID {

            let urlString = URL(string: <urlString>)

            let urlRequest = URLRequest(url: urlString!)

            let config = URLSessionConfiguration.default
            let session = URLSession(configuration: config)

            self.feedSection.isLoadComplete = true
            self.delegate?.collectionViewDidEnd(updatedFeedSection: self.feedSection, indexPath: indexPath)

            let task = session.dataTask(with: urlRequest) { (data, response, error) in

                if error == nil {

                } else {
                    print(error?.localizedDescription as Any)
                }

                guard let responseData = data else {
                    print("Error: did not receive data")
                    return
                }

                do {
                    guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])as? [String: Any] else {
                        print("error trying to convert data to JSON")
                        return
                    }
                    print("success convert data to JSON")

                    DispatchQueue.main.async {

                        self.feedSection.storiesArray? = (todo["data"]! as! Array)
                        self.feedSection.isLoadComplete = true

                        self.collectionView.reloadData()

                        self.collectionView.performBatchUpdates({

                        }, completion: { (complete) in

                            self.collectionViewHeightConstraint.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height + self.collectionView.contentInset.top + self.collectionView.contentInset.bottom
                            self.delegate?.collectionViewDidEnd(updatedFeedSection: self.feedSection, indexPath: indexPath)
                        })
                    }
                } catch  {
                    print("error trying to convert data to JSON")
                    return
                }

            }
            task.resume()
        }
    }