Swift 3.0 使用 Table 视图显示和添加评论

Swift 3.0 Using a Table View to Show and Add Comments

我有兴趣拥有一个用于评论的表格视图(类似于 instagram 评论)。到目前为止,我已经使用自定义单元格为我的集合数组 dataName 中的注释设置了一个 textView。我想知道如何在 tableview 的最后一行设置一个文本字段和按钮,作为输入更多评论的地方。我是否需要为此创建另一个自定义单元格并在 cellForRowAt indexPath 中实现它?

var comments = ["I like this item","Where did you get this?", "I can't believe you found this!", "Hello", "Yay"]  

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

    return comments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell


    cell.commentView.text = dataName[indexPath.row]
    cell.commentView.textColor = UIColor.lightGray
    cell.commentView.isEditable = false
    cell.commentView.isScrollEnabled = false


    return cell
}

您可以通过添加一个包含文本字段和按钮的视图作为表视图的页脚视图来实现您想要的效果。当添加新评论时,您将继续将评论添加到数组,并使用动画重新加载 tableview 或 insertRow。

您已经有了一个名为 "Cell" 的细胞原型。只需添加另一个名为 "Comment" 的单元格原型即可。现在您有两个带有两个不同标识符的单元格原型。如果您在最后一行,请在 dequeue 调用中请求 "Comment" 单元格原型。