可能有来自两个不同阵列的两个原型单元,没有部分?

Possible to have two prototype cells from two different arrays, without sections?

我正在努力实现这一目标,非常感谢您的帮助:

有人在 Swift 中对此有任何代码解决方案,特别是关于如何实现 "numberOfRowsInSection" 和 "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. 

在我试过的 numberofRowsinSection 中:

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 数组 - 要么说 - "cannot index empty buffer",要么说 "array index out of range"。我目前在数组中只有一个评论。不确定这是否导致了问题。

数据在那里,但不会拉入。非常感谢任何帮助!

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