具有索引路径的单元格的iOS Swift UICollectionView布局属性不存在

时间:2017-05-03 09:54:08

标签: ios swift3 uicollectionview nsindexpath

我在我的应用中拥有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
}

0 个答案:

没有答案