3

你好 StackOverflowers!

我试图在满足条件时隐藏单元格 - 在这种情况下,当用户调查评论为空时。数据是从 Firebase 加载的 这是我迄今为止尝试过的:隐藏单元格以及选择它。然后对 selectedRows 使用 heightForRow。但它不起作用。请帮忙!谢谢~~~

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return comment.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CommentsTableCell", for: indexPath) as! CommentsCell
    let surveyCommentList = self.comment[indexPath.row]
    cell.selectionStyle = UITableViewCell.SelectionStyle.none
    if surveyCommentList.surveyComment == "" {
        cell.isHidden = true
        cell.isSelected = true
    } else {
        cell.surveyCommentText.text = surveyCommentList.surveyComment
    }
    return cell
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if let selectedRows = tableView.indexPathsForSelectedRows, selectedRows.contains(indexPath) {
        return 0
    } else {
        return 50
    }
}
4

1 回答 1

1

这是正确的实现:

 // Define a new filtered array from your comments that filters empty Comments
 let filteredComments = comment.filter({!$0.surveyComment.isEmpty)

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredComments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CommentsTableCell", for: indexPath) as! CommentsCell
    let surveyCommentList = self.filteredComments[indexPath.row]
    cell.selectionStyle = UITableViewCell.SelectionStyle.none
    cell.surveyCommentText.text = surveyCommentList.surveyComment
    }
    return cell
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
}
于 2019-09-05T08:38:05.730 回答