突出显示表格单元格中的最高数字

问题描述 投票:0回答:2

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

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

enter image description here

我需要将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
    }
ios swift tableview
2个回答
0
投票

让您的数量确认为可比较

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
            }

希望对您有帮助


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.