【发布时间】:2016-02-16 19:09:26
【问题描述】:
我是使用 Swift 2 和 Xcode 7 编码的新手,我知道有人问过类似的问题,但我无法解决我的问题。 首先,我有一个自定义单元格,其中包含一个开关和 3 个标签。 这些单元格的内容是从二维数组中读取的。 该数组包含一个 1 或 0 代表开关位置,一个数字代表一个月中的哪一天,另外 2 个代表货币价值和描述。 标签文本会根据当前日期或开关是打开还是关闭而改变颜色。 当 Cell 中的开关被切换时,2D 数组会改变以指示更改。
正如其他人所看到的那样,我遇到的问题是当您向上或向下滚动 SWITCH 设置或标签颜色发生变化时。 数组的内容不会改变,表格可以通过移动到另一个屏幕然后再返回来刷新。
我知道这与单元格的重用有关,但我想不出如何将其添加到我的代码中。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return billList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! billTableViewCell
let cellAmountStyle = NSNumberFormatter()
cellAmountStyle.numberStyle = .CurrencyStyle
cell.cellBillDate.text = String(Int(String(billList[indexPath.row][12]))!)
cell.cellBillAmount.text = cellAmountStyle.stringFromNumber(Float(String(billList[indexPath.row][month - 1]))!)
cell.cellBillDescription.text = String(billList[indexPath.row][13])
cell.cellBillPayedOnOff.tag = indexPath.row
cell.delegate = self
if String(billList[indexPath.row][month - 1]) != "0.00" {
if billList[indexPath.row][14] == "0" {
cell.cellBillPayedOnOff.on = false
// Colour Text, RED if past date and Black if OK
if (monthdate >= Int(billList[indexPath.row][12])) {
cell.cellBillDescription.textColor = UIColor.redColor()
}
else {
cell.cellBillDescription.textColor = UIColor.blackColor()
}
}else{
cell.cellBillPayedOnOff.on = true
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
}
}else {
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
// Update other screen information
updateAmounts()
return cell
}
请有人帮我解决这个问题,我相信它会帮助其他有类似问题的人。
【问题讨论】:
-
你能修复代码中的缩进吗?很难阅读。
-
缩进排序,我希望
-
添加了已创建单元格的图像
标签: swift2 ios9 xcode7 custom-cell