【问题标题】:How can I add a "add" button at the end of the visible collection view in iOS如何在 iOS 中可见集合视图的末尾添加“添加”按钮
【发布时间】:2015-01-13 09:15:06
【问题描述】:

我正在尝试在我的集合视图(文件夹)的末尾添加一个按钮来添加一个新单元格(文件夹)。目标是始终在末尾添加一个按钮来添加新单元格(文件夹)。

这是我正在做的事情: 第一次我返回项目数 + 1(有一个额外的单元格用作按钮..)

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    let sections = self.ICEFolderFetchedResultsController!.sections
    let sectionInfo: NSFetchedResultsSectionInfo = sections![section] as NSFetchedResultsSectionInfo
    println("ICEFoldersCVC - numberOfItems: left")
    return sectionInfo.numberOfObjects + 1
}

第二次我尝试用这个方法初始化按钮:

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierFolderCell, forIndexPath: indexPath) as ICEFolderCell
    var numberOfItems = self.collectionView(self.ICEFolderCollectionView, numberOfItemsInSection: 0)
    println("index path row is: \(indexPath.row)")
    println("number of items is: \(numberOfItems-1)")
    if (indexPath.row == numberOfItems - 1) {
        println("initializing button!")
        var addCellButton = UIButton(frame: cell.frame)
        addCellButton.setTitle("Add", forState: UIControlState.Normal)
        addCellButton.addTarget(self, action: "addCellButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
        cell.addSubview(addCellButton)
    }

    println("ICEFoldersCVC - cellForItemAtIndexPath: left")
    return cell
}

第三次我实现了这样的选择器:

    func addCellButtonPressed() {
    UIAlertView(title: "you did it!", message: "Add button was pressed :)", delegate: nil, cancelButtonTitle: "Great!")
}

但是这个从来没有被调用,因为我从来没有看到警报视图......

结果是一个无法触摸的单元格(因为尚未在持久存储中添加数据)。当我触摸单元格时没有任何反应..这是一个屏幕截图..等待..不能..没有足够的声誉..希望我能..sry 伙计们..

我需要一些指导才能使该按钮正常工作...非常感谢! 最好的。

【问题讨论】:

标签: ios uibutton uicollectionview


【解决方案1】:

您好像忘记拨打 show 提醒您了

另外,不是为目标使用实现按钮和方法:

optional func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

如果索引路径来自您的最后一个单元格,则显示警报。

【讨论】:

  • 不确定我是否理解您提出的方法为什么会取代按钮和目标的实现...最后一个单元格能够添加新单元格的目标...
  • 我明白你为什么要这样做。我尝试通过实现建议的方法和一个基本的 if 语句来显示 alerview 如果 indexpath 是最后一个......但该方法从未被调用......我错过了 smg 吗?谢谢..
  • didHighlight 和 didSelect 在第一次单击单元格时被调用。第二次再次调用这两种方法。但是,不会调用“shouldDeselect”和“didDeselect”。
  • 嗯,我认为我得到它...选择另一个时,将取消选择单元格。只有一个单元格,这似乎不起作用......
  • @Andrea.cabral 您想将最后一个单元格添加为添加,这意味着您需要识别何时单击该单元格。你可以用一个按钮来做到这一点,但集合视图已经有一个方法。您可以识别何时选择了此特定单元格(单击)并激活添加功能。正如我所写的,您可以认识到使用optional func collectionView(_ collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) 这将返回用户单击的单元格的索引路径
【解决方案2】:

试试这些步骤

1) 我在 UIalertView 上也找不到任何显示方法。那就写吧

2) 总是在 addCellButtonPressed 中写一些额外的简单日志来检查该方法是否被实际调用。

我没有使用过 swift,但有使用 ObjectiveC 的经验。希望对你有帮助。

【讨论】:

  • 谢谢,确实,我忘记了 show 方法。它起作用了,选择器“addCellButtonPressed”被调用。但是按钮上没有显示标题“添加”......知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 2010-11-11
  • 2020-11-15
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
相关资源
最近更新 更多