【问题标题】:iOS - UITableView add single static cell to dynamic tableiOS - UITableView 将单个静态单元格添加到动态表中
【发布时间】:2017-01-17 07:18:47
【问题描述】:

我有一个由NSFetchedResultsController 控制的UITableView。我想将单个单元格添加到第一行并使该单元格静态。换句话说,会有一个按钮可以打开另一个视图控制器。

到目前为止,我对获取的结果控制器和表格还算满意。现在我有点困惑。我该怎么做?

使用标头也可以,但我不希望此标头一直位于顶部。我希望这个单元格就像聊天面板上的WhatsApp iOS "Create new group" 单元格。

谢谢!

【问题讨论】:

  • 最简单的方法是转到 UITableView -> 将 UIView 作为标题视图拖到常规单元格上方,在其上添加按钮并在 ViewController 中执行操作,它将始终位于顶部,无需编写任何代码来处理这个问题。
  • 您应该在表格视图的标题中添加一个按钮,而不是创建一个单元格。
  • 添加视图是最好的!谢谢@iphonic 我不知道为什么我不这么认为。再次感谢您!

标签: ios objective-c iphone uitableview nsfetchedresultscontroller


【解决方案1】:
var dataArray = ["A","B","C"]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArray.count+1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if indexPath.row == 0
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateNewGroupCell") as! CreateNewGroupCell
        return cell
    }
    else
    {
       // Get the data from Array
          let data = self.dataArray[indexPath.row-1]

       // Logic to show other cells
          let cell = tableView.dequeueReusableCell(withIdentifier: "OtherCell") as! OtherCell
          return cell

       // ....
    }

}

【讨论】:

    【解决方案2】:

    您将需要创建具有从 NSFetchedResultsController +1 获取的行数的 tableview。同样在cellForRowIndex 方法中,您需要添加一个类似indexPath.row == 0 的检查,并在其中进行更改。

    您还必须在该部分中为该按钮添加操作。您还可以为第一行设置不同的自定义tableview。 它可能类似于以下内容:

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
    
      if(indexPath.row==0){
         let cell = tableView.dequeueReusableCell(withIdentifier: "CellWithButton", for: indexPath) as! CellWithButton
      }
      else{
          let cell = tableView.dequeueReusableCell(withIdentifier: "OtherCells", for: indexPath) as! OtherCells
          //here add data for cells from your array
       }  
     return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多