在另一个TableViewCell

时间:2018-03-13 07:46:03

标签: swift uitableview uibutton

我正在为我的iOS应用程序使用评论和回复系统。我在显示和隐藏按钮方面遇到了问题。

我为tableView创建了两个XIB单元,一个是'commentCell',另一个是'replyCell'。在评论单元格中,我有一个按钮(回复),所以当用户点击“回复”时,它应该在indexPath.row + 1处插入一个单元格,在'replyCell'中我有一个textview和两个按钮(Submit)和(Cancel),所以当用户在textview中输入一些文本并点击“提交”时,它应该存储回复文本,用户点击“提交”后文本视图不能编辑,如果用户点击“取消”,则应删除'replyCell '并删除indexPath.row上的行

我能够实现上述所有功能..

现在我的问题是:

当用户点击“commentCell”中的“回复”按钮时,它应隐藏“回复”按钮,我可以通过在“回复”按钮操作中设置(replyBtn.isHidden = true)来执行此操作。但是,当用户点击“replyCell”中的“取消”按钮时,“commentCell”中的“回复”按钮应再次显示,我不知道如何在“commentCell”中显示“回复”按钮在“取消”按钮操作中设置(replyBtn.isHidden = false)。

问题是,我无法访问'replyCell'中的replyBtn属性,因为它是“commentCell”的属性。

怎么做???

我希望你明白我要传达的内容......

这些是我在UICommentTableViewController中的协议和tableView委托方法:

protocol UICommentReply {

    func replyToCommentAtIndexPath(cell: UICommentTableViewCell)
}

protocol UICancelReply {

    func cancelReplytoComment(cell: UICommentReplyTableViewCell)
}

class UICommentTableViewController: UITableViewController,UICommentReply, UICancelReply {

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

        return tableContent.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  {

        let row = indexPath.row
        let post = self.tableContent[row]
        switch post.type {
        case .Comment:
            let comment = post as! Comment
            let commentCell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: indexPath as IndexPath) as! UICommentTableViewCell
            commentCell.delegate = self
            commentCell.level = comment.level
            return commentCell
        case .Reply:

            let replyCell = tableView.dequeueReusableCell(withIdentifier: "replyCell", for: indexPath as IndexPath) as! UICommentReplyTableViewCell
            replyCell.delegate = self

            return replyCell
        default:
            return UITableViewCell()
        }
    }

   func replyToCommentAtIndexPath(cell: UICommentTableViewCell) {

        print(#function)
        if let path = tableView.indexPath(for: cell) {
            let indexPath = NSIndexPath(row: path.row + 1, section: path.section)
            self.tableContent.insert(Reply(), at: indexPath.row)
            self.tableView.insertRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)

    }

    }

    func cancelReplytoComment(cell: UICommentReplyTableViewCell) {

        print(#function)
        if let path = tableView.indexPath(for: cell) {
        let indexPath = NSIndexPath(row: path.row, section: path.section)
            if self.tableContent[indexPath.row].type == .Reply {
                self.tableContent.remove(at: indexPath.row)
                self.tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)

            }
        }
    }

}

在UICommentTableViewCell中:

@IBDesignable class UICommentTableViewCell: UITableViewCell {
 @IBOutlet weak var controlReplyButton: UIButton!

    var delegate: UICommentReply?

    override func awakeFromNib() {

        super.awakeFromNib()
        self.selectionStyle = UITableViewCellSelectionStyle.none

        controlReplyButton.setTitle("Reply", for: .normal)

        self.setup()
    }

    func setup() {

        self.controlReplyButton.addTarget(self, action: #selector(handleReplyButton(sender:event:)), for: UIControlEvents.touchUpInside)
    }

    func handleReplyButton(sender: UIButton, event: UIEvent) {

        controlReplyButton.isHidden = true

        self.delegate?.replyToCommentAtIndexPath(cell: self)
    }
}

在UICommentReplyTableViewCell中:

class UICommentReplyTableViewCell: UITableViewCell {

    @IBOutlet weak var cancelReply: UIButton!

    @IBOutlet weak var postReplyBtn: UIButton!

    @IBOutlet weak var replyTextView: UITextView!

    var delegate: UICancelReply?

    override func awakeFromNib() {

        super.awakeFromNib()
        self.selectionStyle = UITableViewCellSelectionStyle.none

        self.setup()
    }

    func setup() {

        self.cancelReply.addTarget(self, action: #selector(cancelReplyButton(sender:event:)), for: UIControlEvents.touchUpInside)
 }

    func cancelReplyButton(sender: UIButton, event: UIEvent) {

        self.delegate?.cancelReplytoComment(cell: self)

    }
}

这是我到目前为止的代码,并帮助我。

Comment Screen

Comment Reply Screen

2 个答案:

答案 0 :(得分:0)

您首先必须访问commentCell按钮操作中的cancel,然后访问它的replyBtn属性。

由于您尚未共享任何代码,因此我不知道您的代码结构的详细信息。但是,您必须将代码更改为此类

//Where ever the implementation of cancel button action is written in your code
cancelButtonAction {

    //Replace INDEXPATH with the calculated indexPath for your reply cell
    //Replace YOURCUSTOMCELL with appropriate UITableViewCell class
    if let replyCell = tablview.cellForRow(at: INDEXPATH) as? YOURCUSTOMCELL {
        replyCell.replyBtn.isHidden = false
    }
}

<强>更新

cancelReplyToComment方法中,您使用indexPath作为传递的单元格,该单元格是Reply单元格,而不是Comment单元格。您必须查看业务逻辑并从传入的Comment单元格中计算Reply单元格的indexPath。例如:如果您的评论单元格位于回复单元格之后,则cancelReplyToComment方法将变为

func cancelReplytoComment(cell: UICommentReplyTableViewCell) {
    print(#function)
    if let path = tableView.indexPath(for: cell) {
        let indexPath = NSIndexPath(row: path.row, section: path.section)
        if self.tableContent[indexPath.row].type == .Reply {
            self.tableContent.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)

        }
        let indexPathCommentCell = NSIndexPath(row: path.row + 1, section: path.section)
        //Here, 1 is added to row because the assumption is that the comment cell
        //is just after the reply cell. If it was the other way around,
        //you would have to subtract 1. Basically, you need to calculate
        //the difference between your comment and reply cell
        if let commentCell = tableView.cellForRow(at: indexPathCommentCell) as? YOURCOMMENTCELLCLASS {
            //Access your comment cell's button here
        }
    }
}

答案 1 :(得分:-1)

我刚刚为您实施了一个快速简单的回复和评论系统。

我认为它也可以解决您的问题。

您可以从评论单元格中访问隐藏/显示/回复按钮,展开并折叠表格视图并缩进评论。

SwiftCR GitHub

相关问题