让Cell出现并消失,具体取决于TextInput

时间:2017-07-09 19:53:08

标签: ios swift uitableview cell

我有2个原型细胞。

一个代表帖子的所有评论。 这是最初提出的。

如果用户写了“@”符号,则会显示一个与他可以选择链接的用户的tableview。

我的问题是用户的细胞永远不会消失。

如果触摸了一个单元格,或者用户删除了@符号,我希望它们消失。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if (commentTextField.text?.contains("@"))! {
        let cellForUser = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! SuggestUserTableViewCell

        let user = usersSuggestion[indexPath.row]

        cellForUser.userSuggested = user
        return cellForUser
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentTableViewCell
        let comment = comments[indexPath.row]
        let user = users[indexPath.row]

        cell.tapMore.tag = indexPath.row
        cell.comment = comment
        cell.postId = postId
        cell.user = user
        cell.delegate = self
        return cell

    }


}

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

    let caption = commentTextField.text
    let words = caption?.components(separatedBy: CharacterSet.whitespacesAndNewlines)

    for var word in words! {
        if word.hasPrefix("@") {
            word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)
            return usersSuggestion.count

        }
    }
    return comments.count

}

此外,我希望底部有用户的cell2,目前都是从屏幕顶部开始。

我不想让评论消失,只是减少细胞的不透明性。

一旦细胞被触摸或@符号被删除,

cell2就会消失。

提前致谢

更新代码

   @objc func textFieldDidChange() {

    doSearch()

    if let commentText = commentTextField.text , !commentText.isEmpty {
        sendButton.setTitleColor(UIColor.blue, for: UIControlState.normal)

        sendButton.isEnabled = true
        return
    }

    sendButton.setTitleColor(UIColor.lightGray, for: UIControlState.normal)
    sendButton.isEnabled = false
}

func doSearch() {
    let caption = commentTextField.text

    let words = caption?.components(separatedBy: CharacterSet.whitespacesAndNewlines)
    for var word in words! {
        if word.hasPrefix("@") {
            word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)

            self.usersSuggestion.removeAll()
            API.User.suggestUsers(withText: word, completion: { (user) in

                self.usersSuggestion.insert(user, at: 0)
                self.tableView.reloadData()
            })
            self.usersSuggestion.removeAll()
        }else {
            self.usersSuggestion.removeAll()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

而不是检查" @"在CellForRowAt中,我认为您应该使用动作.editingChanged为文本字段添加目标,以便每次发生更改时都会触发检查。 希望它有所帮助。

相关问题