仅在 table 视图中显示相似的比较数据
Display only similar compared data into table view
我的问题与我之前的有点相似,但又有所不同。
有代码说明:
我有 2 个 Json 数组,我尝试在我的代码中比较它们 -> all 和 allUrl。当 array all 包含来自 array allUrl image table 的一些 id 时,行应更改为红色,反之亦然。
而我的新 question:How 我可以在 table 中只显示 "red_icon" 数据吗?这两个 arrays.thanks
的数据应该相似
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=11=][.id] = .timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if cell.textLabel?.text == key.key {
cell.imageView!.image = UIImage(named:"red_icon")
break
}else{
cell.imageView!.image = UIImage(named:"green_icon")
}}
return cell
}
经过以下讨论后更正工作代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=12=][.id] = .timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if all[indexPath.row].id == key.key {
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
cell.imageView!.image = UIImage(named:"red_icon")
cell.isHidden = false
break
}else{
cell.isHidden = true
}}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=12=][.id] = .timestampValue}
for key in listOfStudentsUrl{
if all[indexPath.row].id == key.key{
rowHeight = 49.0
break
}else{
rowHeight = 0.0
}}
return rowHeight
}
所以,您正试图隐藏 TableView 的某些特定单元格。
您可以执行此操作的一种方法是隐藏您的单元格,然后将其高度设置为零。
要隐藏它,请使用 属性 .isHidden
。
要更改它的高度,请重写函数 heightForRowAtIndexPath
.
查看更多:
我的问题与我之前的有点相似,但又有所不同。
有代码说明: 我有 2 个 Json 数组,我尝试在我的代码中比较它们 -> all 和 allUrl。当 array all 包含来自 array allUrl image table 的一些 id 时,行应更改为红色,反之亦然。
而我的新 question:How 我可以在 table 中只显示 "red_icon" 数据吗?这两个 arrays.thanks
的数据应该相似
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=11=][.id] = .timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if cell.textLabel?.text == key.key {
cell.imageView!.image = UIImage(named:"red_icon")
break
}else{
cell.imageView!.image = UIImage(named:"green_icon")
}}
return cell
}
经过以下讨论后更正工作代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=12=][.id] = .timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if all[indexPath.row].id == key.key {
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
cell.imageView!.image = UIImage(named:"red_icon")
cell.isHidden = false
break
}else{
cell.isHidden = true
}}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){[=12=][.id] = .timestampValue}
for key in listOfStudentsUrl{
if all[indexPath.row].id == key.key{
rowHeight = 49.0
break
}else{
rowHeight = 0.0
}}
return rowHeight
}
所以,您正试图隐藏 TableView 的某些特定单元格。 您可以执行此操作的一种方法是隐藏您的单元格,然后将其高度设置为零。
要隐藏它,请使用 属性 .isHidden
。
要更改它的高度,请重写函数 heightForRowAtIndexPath
.
查看更多: