【问题标题】:UICollectionView not working as intendedUICollectionView 未按预期工作
【发布时间】:2015-05-11 20:00:33
【问题描述】:

我是一名新的 iOS 开发人员,如果我遗漏了一些明显的东西,请多多包涵。我想开发一个兼容 iOS 7 和 8 的 swift 应用程序。

我的自定义视图控制器包含一个 UICollectionView 和一个 UITableView。当用户在 Collection 视图中选择一个单元格时,会对 table view 进行适当的更改。我使用自定义 UICollectionViewCell,如下所示。

class CategoryCollectionViewCell: UICollectionViewCell {
  @IBOutlet weak var imageView:UIImageView!
  @IBOutlet weak var textView:UILabel!

  var categoryInfo: String!
}

另外,这是视图控制器的实现:

class FeedViewController: MainPagesViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var tableView:UITableView!
    @IBOutlet weak var collectionView:UICollectionView!

    var shownData:[String] = []
    var selectedCell: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    self.shownData = self.collectionData(self.selectedCell)
    self.collectionView.registerNib(UINib(nibName: "CategoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CategoryCells")
    self.collectionView.allowsMultipleSelection = true
}

//    MARK: Collection View
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CategoryCells", forIndexPath: indexPath) as CategoryCollectionViewCell
    cell.layer.cornerRadius = 5.0
    cell.clipsToBounds = true
    if indexPath.item == 0 {
        cell.textView.text = ""
        cell.imageView.image = UIImage(named: "back")
        cell.userInteractionEnabled = false
        return cell
    }
    cell.imageView.image = nil
    cell.categoryInfo = self.shownData[indexPath.item - 1]
    cell.textView.text = cell.categoryInfo
    return cell;
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.deselectItemAtIndexPath(indexPath, animated: false)
    if(indexPath.item == 0){
        self.shownData = self.collectionData("back")
        collectionView.reloadData()
        self.refreshTableView()
    }
    else {
        self.shownData = self.collectionData(self.selectedCell)
        collectionView.reloadData()
        self.refreshTableView()
    }
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.shownData.count + 1
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if (indexPath.item == 0){
        return CGSize(width: 30, height: 30)
    }
    var sizingCell = (UINib(nibName: "CategoryCollectionViewCell", bundle: nil).instantiateWithOwner(nil, options: nil))[0] as CategoryCollectionViewCell
    sizingCell.textView.text = self.shownData[indexPath.item - 1]
    sizingCell.setNeedsLayout()
    sizingCell.layoutIfNeeded()
    let size = sizingCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    return CGSize(width: size.width + 1, height: 30)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, estimatedItemSize indexPath: NSIndexPath) -> CGSize {
    if (indexPath.item == 0){
        return CGSize(width: 30, height: 30)
    }
    return CGSize(width: 80, height: 30)
}



}

collectionData() 是一个函数,用于设置要在集合视图单元格中显示的标签。 refreshTableView() 对表格视图进行更改。 MainPagesViewController 是一个带有一些变量的简单 UIViewController。此外,集合视图使用水平滚动方向的流布局。

在 iPhone 模拟器中测试应用程序时,第一次触摸单元格调用 didSelectItemAtIndexPath()。但是在使用 reloadData() 重新加载集合视图后,只有一些单元格调用了 didSelectItemAtIndexPath() 并且触摸没有注册到所有单元格。

提前致谢。

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    刚刚发现在cellForItemAtIndexPath中禁用了用户交互时,对于出列的可重用单元格将禁用交互。我只需要为出队的可重用单元重置用户交互。

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2022-01-24
      • 2020-05-15
      • 2014-10-31
      • 2018-02-12
      • 2014-01-20
      • 2015-01-13
      • 2013-08-01
      • 2019-04-13
      相关资源
      最近更新 更多