2017-05-31 76 views
1

我在表格視圖中使用tableviewcell.xib。在我有評論按鈕,如果我點擊,我會導航到另一個頁面,以便我可以發表評論,當我評論後解僱。它會來到tableview頁面,因爲我想更新評論計數值而不用服務調用進行更新。我應該添加這個代碼來更新cell.Please幫助我。在swift 3的tableview中更新tableviewcell中的特定行?

 let indexPath = IndexPath(item: sender.tag, section: 0) 
self.tableView.reloadRows(at: [indexPath], with: .automatic) 

我導航這樣

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 
    let commentPage = storyBoard.instantiateViewController(withIdentifier: "postCommentsPage") as! PostCommentViewController 
    self.present(commentPage, animated: false, completion:nil) 
+0

編輯你的問題,你如何轉移到這個評論屏幕。你也改變了你的數據源,因爲如果你沒有重新加載行,什麼也不做。 –

+0

我也問過你的數據源數組是否也改變過了? –

+0

@NiravD。我不明白你在問什麼。我沒有改變數據源中的任何東西 –

回答

2

你需要做的就是讓一個像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) 
+0

當我滾動tableview的價值恢復回來不顯示更新值 –

+0

@UmaMadhavi你是否也更改了數組中的值,如果在數組中正確更改值,那麼這不應該發生。 –

0

您可以使用NSNotification。在該表視圖的viewDidLoad方法中註冊通知。如下圖所示發送通知之前:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 
    let commentPage = storyBoard.instantiateViewController(withIdentifier: "postCommentsPage") as! PostCommentViewController 
NotificationCenter.default.post(name: Notification.Name(rawValue: myNotificationKey), object: self) 
    self.present(commentPage, animated: false, completion:nil) 

重新裝入通知調用函數中的表視圖。

相關問題