在表格视图的特定单元格中放置一个图标 - Swift
Put an icon in specific cells in tableview - Swift
我正在尝试制作一个联系人列表,并以编程方式在姓氏等于特定字符串的每个姓名附近放置一个星号,如下图所示。
但实际上,问题是当这个星星出现在某个单元格上时,向下或向上滚动时,几乎其他单元格也会出现星星。 (就像冠状病毒一样,到处传播)。
有人能帮我解决这个问题吗?
这是我的 TableView 代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = self.tableArray[indexPath.row]
let cell:SLF_LabelIconCell = tableView.dequeueReusableCell(withIdentifier: SLF_LabelIconCell.className) as? SLF_LabelIconCell ?? SLF_LabelIconCell()
//Add a star near favorite contacts
if item.familyName == "Zaghrini"{
cell.favoriteIcon.isHidden=false
}
return cell
}
乍一看,它显示正确
但是上下滚动之后,出现了更多的星星:
试试这个:
if item.familyName == "Zaghrini" {
cell.favoriteIcon.isHidden = false
} else {
cell.favoriteIcon.isHidden = true
}
我正在尝试制作一个联系人列表,并以编程方式在姓氏等于特定字符串的每个姓名附近放置一个星号,如下图所示。 但实际上,问题是当这个星星出现在某个单元格上时,向下或向上滚动时,几乎其他单元格也会出现星星。 (就像冠状病毒一样,到处传播)。 有人能帮我解决这个问题吗? 这是我的 TableView 代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = self.tableArray[indexPath.row]
let cell:SLF_LabelIconCell = tableView.dequeueReusableCell(withIdentifier: SLF_LabelIconCell.className) as? SLF_LabelIconCell ?? SLF_LabelIconCell()
//Add a star near favorite contacts
if item.familyName == "Zaghrini"{
cell.favoriteIcon.isHidden=false
}
return cell
}
乍一看,它显示正确
试试这个:
if item.familyName == "Zaghrini" {
cell.favoriteIcon.isHidden = false
} else {
cell.favoriteIcon.isHidden = true
}