Ios 如何添加一个“;“加载更多”;当按下该单元格时,会向tableview添加更多行吗?

Ios 如何添加一个“;“加载更多”;当按下该单元格时,会向tableview添加更多行吗?,ios,swift,uitableview,tableviewcell,Ios,Swift,Uitableview,Tableviewcell,我正在尝试创建一个表格视图,最初仅显示5个单元格加上一个“加载更多”单元格,当按下时,将显示更多单元格。我正在下载JSON数据并将其拆分为3个数组,一个用于总数据,一个用于仅前5个单元格的数据,另一个用于从总数据中减去前5个单元格的数据后的剩余数据 我在这里看到了这个问题:并尝试实施他们所做的,但收效甚微。这就是我们目前所拥有的- 非常简单、直接的故事板,有两个不同的单元,第一个是包含JSON数据的“测试单元”,第二个是“扩展单元”,按下时应该显示其余的单元。这是我到目前为止的代码,我省略了很

我正在尝试创建一个表格视图,最初仅显示5个单元格加上一个“加载更多”单元格,当按下时,将显示更多单元格。我正在下载JSON数据并将其拆分为3个数组,一个用于总数据,一个用于仅前5个单元格的数据,另一个用于从总数据中减去前5个单元格的数据后的剩余数据

我在这里看到了这个问题:并尝试实施他们所做的,但收效甚微。这就是我们目前所拥有的-

非常简单、直接的故事板,有两个不同的单元,第一个是包含JSON数据的“测试单元”,第二个是“扩展单元”,按下时应该显示其余的单元。这是我到目前为止的代码,我省略了很多以保持本文的简短,我目前收到一个错误,它被标记为:

  var castArray: [CastData?]? = []
  var first5CastArray: [CastData?]? = []
  var remainderCastArray: [CastData?]? = []

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if let firstArray = first5CastArray {
      return firstArray.count + 1
    } else {
      return 0
    }
     }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row < (first5CastArray?.count)! {
      let testCell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestCell

      testCell.castImageView.image = first5CastImageArray?[indexPath.row]
      return testCell

    } else {
      let expandCell = tableView.dequeueReusableCell(withIdentifier: "expandCell", for: indexPath) as! ExpandCell
      expandCell.moreLabel.text = "More"//Error here -fatal error: unexpectedly found nil while unwrapping an Optional value
      return expandCell
    }
  }

//Below code never tested because of fatal error from cellForRowAt indexPath!!!!!
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if let remainderArray = remainderCastArray {

      for info in remainderArray {
        first5CastArray?.append(info)
      }
    }
expandCell.isHidden = true
       tableview.reload 
      }
var castArray:[CastData?]?=[]
变量first5CastArray:[CastData?]?=[]
var remainderCastArray:[CastData?]?=[]
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果让firstArray=first5CastArray{
返回firstArray.count+1
}否则{
返回0
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果indexPath.row<(first5CastArray?.count){
将testCell=tableView.dequeueReusableCell(标识符为:“testCell”,for:indexath)设为!testCell
testCell.castImageView.image=first5CastImageArray?[indexPath.row]
返回测试单元
}否则{
将expandCell=tableView.dequeueReusableCell(标识符为:“expandCell”,for:indexath)设为!expandCell
expandCell.moreLabel.text=“更多”//Error here-致命错误:在展开可选值时意外发现nil
返回单元
}
}
//下面的代码从未测试过,因为cellForRowAt indexPath出现致命错误!!!!!
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
如果让remainderArray=remainderCastArray{
有关remainderArray中的信息{
first5CastArray?.append(信息)
}
}
expandCell.ishiden=true
tableview.reload
}

我不完全确定我是否用正确的方法来做这件事,任何帮助都是非常感谢的

返回您的故事板,再次选择该单元格,转到“属性检查器”选项卡,在
identifier
字段中键入
expandCell

您确定moreLabel插座已正确地从故事板链接到expandCell类吗?是的,我刚刚检查了您的单元格@swiftyjd的标识符。单元格标识符是正确的,仍然从UITableView中获取单元格初始化的niluse if let,要查看tableView出列是否有问题,或者问题是否与UILabel本身有关,请使用
if let expandCell=tableView.dequeueReusableCell(带标识符:“expandCell”,for:indexath)as?ExpandCell{}