我在我的应用中拥有UICollection View。我的应用程序崩溃错误:
UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000023916> {length = 2, path = 569 - 0}
。如何解决?
注意: 我只对iPhone5 / iPhone5s有这个问题
代码在这里:
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.RecentlyUsed.count
}
func collectionView(_ collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
if(self.RecentlyUsed.count > 0)
{
let nameString = ((self.RecentlyUsed[indexPath.row] as AnyObject).value(forKey: "name") as! String)
let textfield = UITextField() //PT Serif Caption 19.0
textfield.text = nameString
var myTextViewSize = CGSize()
myTextViewSize = textfield.sizeThatFits(CGSize(width: textfield.frame.width,height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: myTextViewSize.width + 10,height: 50)
}
return CGSize(width: 0,height: 0)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CSNewsRecentlyUsedCustomCell
cell.ButtonAction.tag = indexPath.row + 1;
cell.ButtonAction .addTarget(self, action: #selector(buttonselected), for:UIControlEvents.touchUpInside)
cell.ButtonAction.setTitle("\((self.RecentlyUsed[indexPath.row] as AnyObject).value(forKey: "name") as! String)", for: UIControlState.normal)
return cell
}