如何更新tableView单元格?

时间:2015-10-05 05:11:27

标签: ios iphone swift uitableview

我有一个tableView,我想每10秒更新一次。我这样做:

var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "reloadTable", userInfo: nil, repeats: true)

func reloadTable() {
    print("reload")
    self.tableView.reloadData()
}

但它没有正确重新加载。这意味着什么:

是的,它重新加载,但我的数据没有更新,但当我将tableView拖到顶部并离开时,我的tableView单元格数据更新。如何以编程方式实现此效果?

更新

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> onlineUserCell {
    let cell:onlineUserCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! onlineUserCell

    let user = OneRoster.userFromRosterAtIndexPath(indexPath: indexPath)

    cell.username.text = user.displayName

    if user.unreadMessages.intValue > 0 {
        cell.backgroundColor = .orangeColor()
    } else {
        cell.backgroundColor = .whiteColor()
    }
    configurePhotoForCell(cell, user: user)

    cell.avatarImage.layer.cornerRadius = cell.avatarImage.frame.size.height / 2
    cell.avatarImage.clipsToBounds = true

    return cell;
}

2 个答案:

答案 0 :(得分:0)

我不是100%肯定,但如果表格没有在视觉上更新,您可能不会在主线程上调用UI代码。所有UI代码都需要在iOS中的主线程上执行。

查找dispatch_async和/或performSelectorOnMainThread。

答案 1 :(得分:0)

你也需要以编程方式更新数据源.reloadData()方法只是刷新单元格。如果你不刷新数据源(实际数组),则会有不同的。

相关问题