【发布时间】:2016-09-26 18:47:17
【问题描述】:
我有一个水平滚动的集合视图,每个单元格都会在点击时推送到详细视图。当我加载应用程序时,我让它在索引处打印对象。首先,它会加载它应该加载的一个单元格。但是,当我滚动一个空格时,它会打印出两个新的 id,然后开始将最后加载的单元格的数据与屏幕上当前显示的单元格的数据相关联,而现在屏幕上的单元格已经偏离了一个位置。我不知道如何解决这个问题,我最好的猜测是有一些方法可以更好地跟上当前索引,或者 viewDidAppear 中有一些东西可能我丢失了。这是一些代码:
open var currentIndex: Int {
guard (self.collectionView) != nil else { return 0 }
return Int()
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExpandCell", for: indexPath) as! ExpandingCollectionViewCell
let object = self.imageFilesArray[(indexPath).row] as! PFObject
cell.nameLabel.text = object["Name"] as! String
whatObject = String(describing: object.objectId)
print(whatObject)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let object = self.imageFilesArray[(indexPath as NSIndexPath).row]
guard let cell = collectionView.cellForItem(at: indexPath) as? ExpandingCollectionViewCell else { return }
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "EventDetailViewController") as! EventDetailViewController
nextViewController.lblName = nameData
nextViewController.whatObj = self.whatObject
self.present(nextViewController, animated:false, completion:nil)
}
有没有人知道设置当前索引的更好方法,所以我总是将正确的数据推送到下一页?
【问题讨论】:
标签: ios swift didselectrowatindexpath nsindexpath