【问题标题】:Update particular row in tableviewcell in tableview in swift 3?在swift 3中的tableview中更新tableviewcell中的特定行?
【发布时间】:2017-05-31 05:44:39
【问题描述】:

我在表格视图中使用 tableviewcell.xib。在那我有评论按钮,如果我点击它,我将导航到另一个页面,以便我可以评论,当我在评论后关闭时。它将来到 tableview 页面,因为我想更新评论计数值而不用 service call 更新。我应该在哪里添加此代码以更新单元格。请帮助我。

     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)

【问题讨论】:

  • 编辑您的问题,了解您是如何移动到此评论屏幕的。您是否还更改了数据源,因为如果没有,那么重新加载行将无济于事。?
  • 我也问过你的数据源数组,你也改了吗?
  • @NiravD。我不明白你在问什么。我没有更改数据源中的任何内容
  • 您正在使用数组在 tableView 中显示数据您是否更改了该数组以显示新的评论编号,否则如果您重新加载该行它将什么也不做
  • @NiravD 你说如果我在我的第 10 个帖子中更新完整数组意味着需要不断更新

标签: uitableview swift3 xib nsindexpath


【解决方案1】:

您需要做的是创建一个protocolUpdateCommentCount 并使用您的控制器实现该协议,然后在您的PostCommentViewController 中创建一个UpdateCommentCount 类型的实例属性。 , 同样在你的 tableController 中声明一个Int 类型的属性来保存点击行的引用。

protocol UpdateCommentCount {
    func updateComment(with count:Int)
}

现在用 yourController 实现这个 UpdateCommentCount 并添加一个 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)

【讨论】:

  • 当我滚动 tableview 时,值正在恢复而不显示更新的值
  • @UmaMadhavi 您是否也更改了数组中的值,如果数组中的值正确更改,那么这不应该发生。
【解决方案2】:

您可以使用 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)

比在通知调用函数中重新加载表格视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多