满足条件时隐藏单元格

时间:2019-09-05 08:18:26

标签: swift uitableview

你好StackOverflowers!

我正在尝试在满足条件时隐藏单元格-在这种情况下,当用户调查评论为空时。数据是从Firebase加载的到目前为止,这是我尝试过的方法:隐藏单元并选择它。然后为选定的行跟随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
    }
}

1 个答案:

答案 0 :(得分:2)

这是正确的实现:

 // 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
}