【问题标题】:UITableView Custom cell information changes when scrollingUITableView 滚动时自定义单元格信息变化
【发布时间】: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


【解决方案1】:

是的,您已经清楚地确定了这个原因是因为可重复使用的单元格。所以答案是你应该为单元格中的每个元素赋予颜色。问题是您只为某些部分设置颜色。如果您像下面这样更正了您的代码,您就可以实现您想要的。

注意

请为每个条件状态提供适当的颜色。只要给 你应该更正什么。

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.lightGrayColor()
              cell.cellBillAmount.textColor = UIColor.lightGrayColor()
              cell.cellBillDate.textColor = UIColor.lightGrayColor()
          }
          else {
              cell.cellBillDescription.textColor = UIColor.lightGrayColor()
              cell.cellBillAmount.textColor = UIColor.lightGrayColor()
              cell.cellBillDate.textColor = UIColor.lightGrayColor()
          }
      } else {
          cell.cellBillPayedOnOff.on = true
          cell.cellBillDescription.textColor = UIColor.lightGrayColor()
          cell.cellBillAmount.textColor = UIColor.lightGrayColor()
          cell.cellBillDate.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        
}

【讨论】:

  • 谢谢你,是的,我只是为开关添加了相同的条件,一切都很好。感谢您的及时帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多