最新消息:20210917 已从crifan.com换到crifan.org

[已解决]swift中有时候tableview的reloadData不生效

iOS crifan 4265浏览 0评论

代码:

class CustomerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate, UITextFieldDelegate,MGSwipeTableCellDelegate {
//class CustomerViewController: TabSubViewController {
    var tableView:UITableView
    var customerSectionList:[CustomerSection]
    func updateCustomer(customerId:String,followupState:CustomerFollowup){
        for curSection in self.customerSectionList {
            if curSection.customerItem.id == customerId {
                curSection.customerItem.followupState = followupState
                curSection.customerItem.updateTime = NSDate()
                self.customerSectionList = self.customerSectionList.sort({$0.customerItem.updateTime > $1.customerItem.updateTime})
                break
            }
        }
        dispatchMain_async({
            self.tableView.reloadData()
        })
    }
}
            SingletonCustomerVC().updateCustomer(self.addMemoCustomerId, followupState:self.addMemofollowup )

但是:

SingletonCustomerVC()中的CustomerViewController的

self.tableView.reloadData()

时候,却不起效果。

但是好像Xcode调试,第一次运行程序去调试的时候,reloadData好像就是可以的。

总的说,现象是:

列表的更新数据源:

self.tableView.reloadData()

有时候生效,有时候不生效。

[解决过程]

1.swift reloaddata not working

ios – Swift – ReloadData does not work – Stack Overflow

ios – self.tableView.reloadData() not working in Swift – Stack Overflow

ios – self.tableview.reloaddata() not working in swift at all – Stack Overflow

ios – TableView.reloadData() is not working ? (SWIFT) – Stack Overflow

ios – ReloadData not working Swift with Alamofire – Stack Overflow

    public func reloadData() // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks
    @available(iOS 3.0, *)
    public func reloadSectionIndexTitles() // reloads the index bar.

好像是:

-》当时,在当前所关心的,客户页面,的子页面

-》所以当时对于客户页面的

self.tableView.reloadData

的时候,当前的vc还处在invisible,不可见,没有显示呢

-》所以估计是reloadData没有生效的原因

-》考虑去对应的vc的viewDidAppear中,添加对应的标记位,表示别处更新了数据源datasource

如果此处要显示页面了,就应该去更新对应的数据,去reloadData

最后的代码改为:

class CustomerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, … {
    var tableView:UITableView
    var customerSectionList:[CustomerSection]
   var needReloadData:Bool
    init(){
        self.tableView = UITableView(frame: CGRectZero, style: .Plain)
        self.customerSectionList = [CustomerSection]()
        self.needReloadData = false
        super.init(nibName: nil, bundle: nil)
}
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if self.needReloadData {
            self.needReloadData = false
            self.tableView.reloadData()
        }
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.customerSectionList.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let customerSection = self.customerSectionList[indexPath.row]
        return cell
}
    func updateCustomer(customerId:String, followupState:CustomerFollowup){
        var isUpdated = false
        for curSection in self.customerSectionList {
            if curSection.customerItem.id == customerId {
                curSection.customerItem.followupState = followupState
                …
                isUpdated = true
                break
            }
        }
        if isUpdated {
            if isCurrentShowingVc(self) {
                dispatchMain_async({
                    self.tableView.reloadData()
                })
            } else {
                //need later, show current vc, then reload data
                self.needReloadData = true
            }
        }
    }

即可保证:

数据源更新后,可以正常的更新对应的列表了:

1.更新数据时,是当前列表页面时,则直接更新数据源,去reloadData

2.更新数据时,不是当前列表页面时,则标记之后要去更新数据源,然后在viewDidAppear中,去更新更新数据源reloadData

转载请注明:在路上 » [已解决]swift中有时候tableview的reloadData不生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.179 seconds, using 23.41MB memory