【发布时间】: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