【问题标题】:tableview reload data method being called, but not executing正在调用 tableview 重新加载数据方法,但未执行
【发布时间】:2014-10-26 13:25:31
【问题描述】:

我的应用中有一个表格视图,它必须显示聊天消息列表。我将聊天消息加载到 NSMutableArray 中,在将数据加载到数组后,我需要更新 tableview。

这是我正在使用的代码:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    ("reloading data")
    self.tableView.reloadData()
})

但它会打印出reloading data,但不会更新/重新加载表格视图。

有什么想法吗?

更新

我意识到它没有调用这个方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    println("running")
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
    println(listDataArray[indexPath.row])
    cell.textLabel?.text = (listDataArray[indexPath.row] as NSString)
    return cell
}

但如果我尝试删除它或将其注释掉,他们会说该类不符合 UITableViewDataSource 的协议。

那么它怎么能不调用该方法呢? (NOTE: it is not printing "running" either)

更新 2

这里是 tableview 方法的代码(注意,我在上面发布的时候稍微改变了那个)

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    if(listDataArray.count > 0) {
        return listDataArray.count
    } else {
        return 0
    }
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

    if !(cell != nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    }
    cell?.textLabel?.text = listDataArray[indexPath.row] as? String

    return cell
}

【问题讨论】:

  • 也许你的数组没有像你想象的那样被更新,或者你的 cellForRowAtIndexPath 是错误的......这可能是很多事情......
  • 我知道数组正在更新,因为如果我打印出来,新内容就在那里
  • [self.tableView reloadData];
  • @adnan 不,这是 Swift。
  • @JoshHarington 你能发布你的 cellForRowAtIndexPath 方法吗?你在重复使用细胞吗?

标签: ios uitableview swift


【解决方案1】:

愚蠢的问题,但你不会忘记设置dataSource属性吗?

【讨论】:

  • 我删除了数据源属性,清理了项目,然后重新设置。似乎有效,但我不知道为什么?
  • Xcode 可能使用了旧版本的编译代码,其中不包括数据源设置。这就是清洁有帮助的原因。
【解决方案2】:

如果你使用的是 swift ,那么你不应该使用 NSMutableArray。尝试查看 swift 提供的方法并使用它。

例如 listDataArray 应该被初始化为

 var listDataArray = ["TYPE OF OBJECT"] ()

【讨论】:

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