【问题标题】:Highlight the highest number in tableview cell突出显示表格视图单元格中的最高数字
【发布时间】:2020-09-08 17:07:01
【问题描述】:

我有一个简单的表格视图,单元格中有数字。一个单元格 - 一个数字。如果单元格编号是其他表格视图单元格编号中最高的,我想将单元格文本加粗。

例如,我有一个带有数字的表格视图:

我需要将 99 单元格文本加粗,因为 99 是所有表格视图单元格中的最高数字。如何做到这一点?

我的数据源数组:

class Exercise: NSObject, Codable {
  var name = ""
  var reps = [Count]()
}

class Count: NSObject, Codable {
  var reps: Double
  var date = Date()

    init(reps: Double) {
    self.reps = reps
    super.init()
    }

}

还有我的代码:

class Counts: UITableViewController, CountsDetailViewControllerDelegate {

var countExercise: Exercise!

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countExercise.reps.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Count", for: indexPath)

        let item = countExercise.reps[indexPath.row]

        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .short

        cell.textLabel?.text = item.reps.removeZerosFromEnd()
        cell.detailTextLabel?.text = formatter.string(from: item.date)

        return cell
    }

【问题讨论】:

  • 你的数据源数组是什么?
  • 我已添加到问题中。
  • 与您的问题无关,但每次调用 cellForRowAt 时您都在创建一个新的 DateFormatter
  • 那么放置 DateFormatter 的更好的地方是什么?

标签: ios swift uitableview uiviewcontroller


【解决方案1】:

让您的 Count 确认为 Comparable

class Count: NSObject, Codable, Comparable {
        static func < (lhs: Count, rhs: Count) -> Bool {
            lhs.reps < rhs.reps
        }

      var reps: Double
      var date = Date()

        init(reps: Double) {
        self.reps = reps
        super.init()
        }

    }   

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "Count", for: indexPath)

     let item = countExercise.reps[indexPath.row]

      let formatter = DateFormatter()
          formatter.dateStyle = .short
          formatter.timeStyle = .short


       if let count = countExercise.reps.max(), 
              count.reps == item.reps {
                    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 26)
                } else {
                    cell.textLabel?.font = UIFont.systemFont(ofSize: 26)
                }

          cell.textLabel?.text = item.reps.removeZerosFromEnd()
          cell.detailTextLabel?.text = formatter.string(from: item.date)

                return cell
            }

希望对你有帮助

【讨论】:

    【解决方案2】:

    您可以向视图控制器添加一个属性,以跟踪数组中所有 Count 对象中的最高 reps 计数:

    var highestRep: Double? {
    
      // We can map over all reps to "extract" each individual `reps` count.
      // This leaves us with an array of `Double` values. We can then call 
      // `max()` on it to get the highest value:
    
      return countExercise.reps.max{ $0.reps < $1.reps }?.reps
    }
    
    // Now that we know which is the highest number, you can format
    // your cell's text accordingly, when setting up your cell:
    
    if highestRep == item.reps {
      // Bold text
    } else {
      // Regular text
    }
    

    max() 方法返回一个Optional 值,这就是为什么新属性的类型为Double?。如果您有一个空数组,它将返回nil

    【讨论】:

    • 比较之前不需要解开最高代表
    • 不客气。顺便说一句,您可以避免使用 max(by:) 两次迭代集合并将谓词传递给它countExercise.reps.max{$0.reps &lt; $1.reps}?.reps
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多