【问题标题】:Swift tableview cell reuse issueSwift tableview 单元格重用问题
【发布时间】:2016-09-03 05:30:50
【问题描述】:

今天早上我正在画一个空白,可以使用一些指导。

我正在使用自定义表格单元格填充带有字典数组的表格视图。字典中的键值对之一是 [“Time”],它在 dicts 数组中表示为:

"时间": "12am", “时间”:“凌晨 1 点”, “时间”:“凌晨 2 点”, “时间”:“凌晨 3 点”等等……

这是我想做的。

如果当前时间是凌晨 1 点(例如),我想更改该单元格的背景颜色。我使用下面粘贴的代码部分工作。当表格视图加载时,正确的单元格被突出显示,但是当我在表格视图中上下滚动时,我看到其他行被突出显示。我假设这与细胞的重复使用方式有关。

附加信息: 1)“项目”是我的字典数组 2) currentTime() 只是一个小函数,它以这种格式(“1pm”)返回一天中的当前时间

有人能指出我正确的方向吗?

亲切的问候, 达林

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:schedTableCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! schedTableCell
    cell.selectionStyle = .None

    // Highlight row where time value is equal to current time - THIS NEEDS WORK
    let showTime = self.items[indexPath.row]["time"] as! String
    if showTime == currentTime() {
        cell.backgroundColor = UIColor.greenColor()
    }

    cell.showTime.text = self.items[indexPath.row]["time"] as? String
    cell.showName.text = self.items[indexPath.row]["show"] as? String
    cell.showHost.text = self.items[indexPath.row]["host"] as? String

    return cell
}

【问题讨论】:

    标签: arrays swift uitableview dictionary


    【解决方案1】:

    确实,这是一个重用问题。当你这样做cell.backgroundColor = UIColor.greenColor() 时,你改变了单元格的背景颜色。但是当单元格被重用时,你不会重置背景颜色,所以它保持绿色!你可以这样修复它(如果white 是你的正常颜色):

       if showTime == currentTime() {
            cell.backgroundColor = UIColor.greenColor()
        } else {
            cell.backgroundColor = UIColor.whiteColor()
        }
    

    【讨论】:

    • 天啊。不敢相信我忽略了这一点。非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2014-12-16
    • 2017-05-03
    相关资源
    最近更新 更多