可能有两个原型细胞来自两个不同的阵列,没有部分



我花了很长时间试图实现这一目标,因此非常感谢任何帮助:

  • 第一个表是用户帖子,当选择一个项目时,第二个表视图显示所选的用户帖子,下面是该帖子的任何评论(如果有的话)(如Facebook)。

  • 我有两个NO切片的原型细胞。

  • 我想从一个数组中输入第一行(userPost)
  • 然后,第二个原型单元格将包含来自单独注释数组的任何数据(如果有的话),该数组将是第一行之外的所有行。
    • 目前,两个原型单元都使用相同的自定义单元类和不同的出口,尽管我最初有两个自定义单元类,但它似乎不适用于我尝试的代码示例
  • 我需要两个独立的数组,因为它们来自cloudkit中的两个不同的表。我不想在提取userPost数据以显示在第一个表视图中的同时提取所有评论数据。

    我在SO上找到了一些示例问题,但没有一个适用于我的情况,因为它们略有不同。没有一个是在迅速。我可以处理部分,但我不能在接口生成器中显示超过一个部分,在那里我可以每个部分有一个原型单元。我真的不想有章节,因为我不需要标题。

有人在Swift中对此有任何代码解决方案,特别是如何实现"numberOfRowsInSection"one_answers"cellForRowAtIndexPath"?

我为"cellForRowAtIndexPath"尝试了这两个例子,但没有成功:

 //  var cell: TableViewCell

  if indexPath.row == 0 {
        let cellPost = tableView.dequeueReusableCellWithIdentifier("CellPost", forIndexPath: indexPath) as TableViewCell
// custom cell class
        let record = userPost[indexPath.row]
        cellPost.postDescription.text = record.description
        cellPost.userName.text = record.username

        if record.postPic != nil {
            cellPost.postPic.image = record.postPic!
        }

        cell = cellPost
       }  else  {

        let cellComment = tableView.dequeueReusableCellWithIdentifier("CellComment", forIndexPath: indexPath) as TableViewCell
        let recordComment = commentResults[indexPath.row]
        // could it be that its trying to subscript 1, but there's only data at 0?
        cellComment.comment.text = recordComment.userComment
        cellComment.commentUsername.text = recordComment.username
      cell = cellComment

   }

    return cell

我尝试了这个,但没有运气:

var identifier: String!
if indexPath.row == 0 {
   identifier == "CellPost"
} else {
   identifier == "CellComment"
}
 // then I configure the cell using the identifier as parameter. 

在我尝试过的RowsinSection的数量中:

return feedResults.count + commentResults.count
//OR
return commentResults.count + 1
// OR
 if commentResults.count > 0 && feedResults.count > 0 {
        return feedResults.count + commentResults.count
    } else {
        return feedResults.count
    }

我不断得到的错误涉及第二个commentResults数组——要么说——"不能索引空缓冲区",要么说"数组索引超出范围"。此时数组中只有一条注释。不确定这是否是问题的原因。

数据在那里,但它不会把它拉进来。任何帮助都将不胜感激!

当然,这是可能的。在numberOfRowsInSection中,返回评论的计数(我假设你在一个数组中有这些)加一(对于帖子)。在cellForRowAtIndexPath中,使用if-else子句,如果indexPath.row等于零,则返回一种单元格,如果不是,则将另一种类型的单元格出列。

最新更新