你需要做的就是讓一個像UpdateCommentCount
和實施協議與你的控制器,你是有這個tableView
後,在您PostCommentViewController
使UpdateCommentCount
類型的一個實例的屬性,也在你tableController聲明的一個屬性鍵入Int
以保存所點擊的行的引用。
protocol UpdateCommentCount {
func updateComment(with count:Int)
}
現在實現這個UpdateCommentCount
與yourController並添加一個Int
財產持有竊聽行的參考和設置您的按鈕操作中您所處的PostCommentViewController
class YourController: UIViewController, UpdateCommentCount, UITableViewDelegate, UITableViewDataSource {
var currentCommentRow = 0
//Your other methods
func commentButtonAction(_ sender: UIButton) {
currentCommentRow = sender.tag
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let commentPage = storyBoard.instantiateViewController(withIdentifier: "postCommentsPage") as! PostCommentViewController
commentPage.commentDelegate = self
self.present(commentPage, animated: false, completion:nil)
}
func updateComment(with count:Int) {
let dic = yourArray[self.currentCommentRow]
dic["commentCount"] = count
yourArray[self.currentCommentRow] = dic
let indexPath = IndexPath(item: self.currentCommentRow, section: 0)
self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
現在PostCommentViewController
宣佈一個實例屬性名爲commentDelegate
,類型爲UpdateCommentCount
,並且在成功發佈評論後,只需調用其委託方法即可。
var commentDelegate: UpdateCommentCount
成功發佈新評論後致電updateComment
。
commentDelegate.updateComment(with: newCount)
編輯你的問題,你如何轉移到這個評論屏幕。你也改變了你的數據源,因爲如果你沒有重新加載行,什麼也不做。 –
我也問過你的數據源數組是否也改變過了? –
@NiravD。我不明白你在問什麼。我沒有改變數據源中的任何東西 –