【问题标题】:Set last tableView cell as constant cell among variable cells in Swift将最后一个 tableView 单元格设置为 Swift 中可变单元格中的常量单元格
【发布时间】:2017-02-24 17:27:30
【问题描述】:

正如标题所说,我想在 tableView 中创建一个不变的单元格,其中单元格是可变的。我希望这个牢房是最后一个。实际上,我所有的单元格都是用来自 Firbase 的数据创建的。最后一个常数单元应始终存在。感谢您的帮助。

【问题讨论】:

    标签: swift xcode uitableview cell


    【解决方案1】:

    你可以试试这样的:

    您只能更改某个 indexPath 处的单元格(在您的情况下为最后一个),然后按照您的意愿进行设置。

    这是一个例子:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            if ((indexPath as NSIndexPath).row == 8 {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "lastCell", for: indexPath) as! lastCellClass
    
              //your code
    
              return cell
    
            }else{
    
               let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as! dataCellClass
    
              //your code
    
              return cell
    
            }
    
    
        }
    

    不要忘记从零开始的单元格(indexPath.row 8)是第 9 个单元格。

    注意:如果您的 numberOfRows 可以更改,您可以这样做:

     if ((indexPath as NSIndexPath).row == (yourData.count - 1){ 
       //Here you will get your last cell
    
    }
    

    【讨论】:

    • 如何返回多个单元格类型?我必须返回我的常量单元格和其他变量单元格...寻求帮助。
    • @greenpoisononeTV 我更新了我的答案以返回多个单元格。让我知道它是否有效:)
    • Value of type 'UICollectionView' has no member 'dequeueReusableCell'
    • ` 让 Cell = collectionView.dequeueReusableCellWithReuseIdentifier("yourcell", forIndexPath: indexPath) as! lastCell 有效吗?
    • 对不起,我把磨损的功能放在那里,现在它应该可以工作了。 :) @greenpoisononeTV
    【解决方案2】:

    是的,您可以通过多种方式实现它。

    这是我的方法。我将为它创建新的 tableview 部分。我将始终保持行数不变。 在 indexpath 的行单元格中,我将检查该部分的条件并返回相应的常量单元格。

    【讨论】:

    • 但是如果我创建一个新部分,最后一个单元格将在其他单元格之后向下。要知道,如果一行 4 个单元格中有 3 个单元格,则常数单元格不会在最后一行的位置。英语不是我的母语,所以我希望你能理解我...
    • 第 0 节中有常量单元格,第 1 节中有其他单元格。
    • 是的,但部分是分开的。我想要第一部分同一行中的最后一个单元格...
    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多