评论系统的实施(Facebook喜欢)

时间:2017-06-06 13:02:49

标签: ios swift uitableview uikit

我正在寻找一种创建评论系统的方法,其行为类似于Facebook的评论后部分。

现在我有这个结构:

Structure

但我还需要实施对评论的回复和对回复的回复等。 应该采取什么措施来实现与Facebook相同的行为?

1 个答案:

答案 0 :(得分:1)

要实施滑动到replydelete以及其他内容,请使用此库: MGSwipeTableCell

对于回复和删除,请执行以下操作:

private func addFuncButtons(to cell: CommentCell, at row: Int) {
  let currentUserId = User.getCurrentUserId()

  if (cell.comment.userId == currentUserId // if its current user comment
     || userId! == currentUserId) // if current user is post author
     && cell.comment.key != "" { // cant delete desc
     cell.rightButtons = [
        MGSwipeButton(title: "", icon: UIImage(named:"delete.png"), backgroundColor: .red) {
           (sender: MGSwipeTableCell!) -> Bool in
           self.removeCell(cell, at: row)
           return true
        },
        MGSwipeButton(title: "", icon: UIImage(named:"reply.png"), backgroundColor: .darkGray) {
           (sender: MGSwipeTableCell!) -> Bool in
           self.replyToUser(with: cell.userNickName.currentTitle!)
           return true
        }
     ]
  } else {
     // add only reply button
     cell.rightButtons = [
        MGSwipeButton(title: "", icon: UIImage(named:"reply.png"), backgroundColor: .darkGray) {
           (sender: MGSwipeTableCell!) -> Bool in
           self.replyToUser(with: cell.userNickName.currentTitle!)
           return true
        }
     ]
  }

  cell.rightSwipeSettings.transition = .rotate3D
}

操作:

private func removeCell(_ cell: CommentCell, at row: Int) {
  removeCellFromTable(cell, at: row)
  removeCellFromDataBase(cell)
}

private func removeCellFromTable(_ cell: CommentCell, at row: Int) {
   comments.remove(at: row)
   tableView.reloadData()
}

private func removeCellFromDataBase(_ cell: CommentCell) {
   Comment.remove(cell.comment, from: post)
}

private func replyToUser(with login: String) {
   newCommentTextField.text = newCommentTextField.text?.appending(" @" + login)
}

就像那样。

希望有所帮助