Ios 高亮显示tableview单元格中的最大数字
我有一个简单的表格视图,单元格中有数字。一个单元格-一个数字。如果单元格编号是其他表视图单元格编号中最高的,我希望将单元格文本设置为粗体 例如,我有一个带有数字的tableview: 我需要将99单元格文本加粗,因为99是所有表视图单元格中的最大数字。如何做到这一点 我的数据源阵列:Ios 高亮显示tableview单元格中的最大数字,ios,swift,uitableview,uiviewcontroller,Ios,Swift,Uitableview,Uiviewcontroller,我有一个简单的表格视图,单元格中有数字。一个单元格-一个数字。如果单元格编号是其他表视图单元格编号中最高的,我希望将单元格文本设置为粗体 例如,我有一个带有数字的tableview: 我需要将99单元格文本加粗,因为99是所有表视图单元格中的最大数字。如何做到这一点 我的数据源阵列: class Exercise: NSObject, Codable { var name = "" var reps = [Count]() } class Count: NSObject, Codab
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
}
使您的计数确认为可比
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
}
类计数:NSObject、可编码、可比较{
静态函数<(左:计数,右:计数)->Bool{
左S.reps<右S.reps
}
var代表:双
变量日期=日期()
初始(重复次数:两次){
self.reps=reps
super.init()
}
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“Count”,用于:indexath)
让item=countExercise.reps[indexPath.row]
let formatter=DateFormatter()
formatter.dateStyle=.short
formatter.timeStyle=.short
如果let count=countExercise.reps.max(),
count.reps==item.reps{
cell.textLabel?.font=UIFont.boldSystemFont(尺寸:26)
}否则{
cell.textLabel?.font=UIFont.systemFont(大小:26)
}
cell.textLabel?.text=item.reps.removeZerosFromEnd()
cell.detailTextLabel?.text=formatter.string(from:item.date)
返回单元
}
我希望这有助于您确认您的数量
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
}
类计数:NSObject、可编码、可比较{
静态函数<(左:计数,右:计数)->Bool{
左S.reps<右S.reps
}
var代表:双
变量日期=日期()
初始(重复次数:两次){
self.reps=reps
super.init()
}
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“Count”,用于:indexath)
让item=countExercise.reps[indexPath.row]
let formatter=DateFormatter()
formatter.dateStyle=.short
formatter.timeStyle=.short
如果let count=countExercise.reps.max(),
count.reps==item.reps{
cell.textLabel?.font=UIFont.boldSystemFont(尺寸:26)
}否则{
cell.textLabel?.font=UIFont.systemFont(大小:26)
}
cell.textLabel?.text=item.reps.removeZerosFromEnd()
cell.detailTextLabel?.text=formatter.string(from:item.date)
返回单元
}
我希望它能帮助您您可以向视图控制器添加一个属性,以跟踪数组中所有
计数对象中最高的重复次数
计数:
var highestRep:Double?{
//我们可以将所有代表映射到“提取”每个单独的“代表”计数。
//这就给我们留下了一个'Double'值数组。然后我们可以调用
//“max()”,以获取最大值:
return countExercise.reps.max{$0.reps<$1.reps}.reps
}
//现在我们知道了最大的数字,您可以格式化
//设置单元格时,相应地显示单元格文本:
如果highestRep==item.reps{
//粗体文本
}否则{
//常规文本
}
max()
方法返回一个Optional
值,因此新属性的类型为Double?
。如果您有一个空数组,它将返回nil
您可以向视图控制器添加一个属性,以跟踪数组中所有count
对象中最高的rep
计数:
var highestRep:Double?{
//我们可以将所有代表映射到“提取”每个单独的“代表”计数。
//这就给我们留下了一个'Double'值数组。然后我们可以调用
//“max()”,以获取最大值:
return countExercise.reps.max{$0.reps<$1.reps}.reps
}
//现在我们知道了最大的数字,您可以格式化
//设置单元格时,相应地显示单元格文本:
如果highestRep==item.reps{
//粗体文本
}否则{
//常规文本
}
max()
方法返回一个Optional
值,因此新属性的类型为Double?
。如果您有一个空数组,它将返回nil
您的数据源数组是什么?我添加到了问题中。与您的问题无关,但每次调用cellForRowAt时,您都在创建一个新的日期格式化程序。放置日期格式化程序的最佳位置是什么?您的数据源数组是什么?我添加到了问题中。与您的问题无关,但您是每次调用cellForRowAt时都要创建一个新的日期格式化程序。放置日期格式化程序的最佳位置是什么?无需在比较之前展开highestRep。欢迎使用。顺便说一句,您可以避免使用max(by:)对集合进行两次迭代,并向它传递一个谓词countExercise.reps.max{$0.reps<$1.reps}?.reps
在比较之前无需展开highestep。欢迎使用。顺便说一句,您可以避免使用max(by:)重复集合两次,并将谓词传递给它countExercise.reps.max{$0.reps<$1.reps}?.reps