iOS tableview 单元格按钮选择奇怪的行为
iOS tableview cell button selection strange behavior
在表格视图中,每个单元格都有一个书签图像按钮。点击它时,它会变成 selected 状态并显示图像(红色书签图标)。我有委托方法将带有 selected 按钮的单元格中的信息添加到数组中:
protocol CustomPoetCellDelegate {
func cell(_ cell: CustomPoetCell, didTabFavIconFor button: UIButton)
}
class CustomPoetCell: UITableViewCell {
@IBOutlet weak var poetNameLabel: UILabel!
@IBOutlet weak var verseCountLabel: UILabel!
@IBOutlet weak var periodLabel: UILabel!
@IBOutlet weak var iconButton: UIButton!
var delegate: CustomPoetCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
iconButton.setImage(UIImage(named: "icons8-bookmark-50-red.png"), for: .selected)
iconButton.setImage(UIImage(named: "icons8-bookmark-50.png"), for: .normal)
iconButton.isSelected = false
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func favIconTapped(_ sender: UIButton) {
delegate?.cell(self, didTabFavIconFor: sender)
}
}
在 tableViewController 中:
func cell(_ cell: CustomPoetCell, didTabFavIconFor button: UIButton) {
if !button.isSelected {
let indexPathRow = tableView.indexPath(for: cell)!.row
if isFiltering() {
favoritePoets.append(searchResults[indexPathRow])
button.isSelected = true
} else {
favoritePoets.append(poets[indexPathRow])
button.isSelected = true
}
} else {
button.isSelected = false
favoritePoets = favoritePoets.filter { [=12=].arabicName != cell.poetNameLabel.text}
}
}
isFiltering() 方法检查 searchBar 是否在处理中。
现在,如果 isFiltering() 为假,一切正常,但如果 isFiltering() 为真(即在 tableView 中搜索名称),当我点击特定的单元格按钮 select 它然后单击取消以关闭搜索栏,其他单元格的图标也被 selected,尽管只有我点击的那个被添加到数组中。在视图中来回导航时,错误的 selection 消失了,只有正确的 selected。
知道发生了什么事吗?
提前致谢。
问题是 UITableView 重新使用单元格来优化滚动性能。因此,它将不再显示的单元格与即将显示的新单元格一起使用。因此,您需要在单元格处于 updated/reused 时设置单元格的状态。
每次都会调用以下函数。所以你需要在这里设置选中或未选中状态的单元格属性。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.indexPath = indexPath //Create an indexPath property in your cell and set it here
//This will be required when you call the delegate method
//configure your cell state
return cell
}
您可以通过更改您已经调用的委托方法中的特定 属性 来更新 favoritePoets 数组中的模型,例如
favoritePoets[indexPath.row].selected = true or false
要访问 indexPath,您需要按照上面的代码进行设置。然后将其作为参数传递到您的委托方法中。现在这个更新的 属性 将帮助您在 cellForRowAt 函数中设置状态。
希望对您有所帮助:)。欢迎评论。
在表格视图中,每个单元格都有一个书签图像按钮。点击它时,它会变成 selected 状态并显示图像(红色书签图标)。我有委托方法将带有 selected 按钮的单元格中的信息添加到数组中:
protocol CustomPoetCellDelegate {
func cell(_ cell: CustomPoetCell, didTabFavIconFor button: UIButton)
}
class CustomPoetCell: UITableViewCell {
@IBOutlet weak var poetNameLabel: UILabel!
@IBOutlet weak var verseCountLabel: UILabel!
@IBOutlet weak var periodLabel: UILabel!
@IBOutlet weak var iconButton: UIButton!
var delegate: CustomPoetCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
iconButton.setImage(UIImage(named: "icons8-bookmark-50-red.png"), for: .selected)
iconButton.setImage(UIImage(named: "icons8-bookmark-50.png"), for: .normal)
iconButton.isSelected = false
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func favIconTapped(_ sender: UIButton) {
delegate?.cell(self, didTabFavIconFor: sender)
}
}
在 tableViewController 中:
func cell(_ cell: CustomPoetCell, didTabFavIconFor button: UIButton) {
if !button.isSelected {
let indexPathRow = tableView.indexPath(for: cell)!.row
if isFiltering() {
favoritePoets.append(searchResults[indexPathRow])
button.isSelected = true
} else {
favoritePoets.append(poets[indexPathRow])
button.isSelected = true
}
} else {
button.isSelected = false
favoritePoets = favoritePoets.filter { [=12=].arabicName != cell.poetNameLabel.text}
}
}
isFiltering() 方法检查 searchBar 是否在处理中。
现在,如果 isFiltering() 为假,一切正常,但如果 isFiltering() 为真(即在 tableView 中搜索名称),当我点击特定的单元格按钮 select 它然后单击取消以关闭搜索栏,其他单元格的图标也被 selected,尽管只有我点击的那个被添加到数组中。在视图中来回导航时,错误的 selection 消失了,只有正确的 selected。
知道发生了什么事吗?
提前致谢。
问题是 UITableView 重新使用单元格来优化滚动性能。因此,它将不再显示的单元格与即将显示的新单元格一起使用。因此,您需要在单元格处于 updated/reused 时设置单元格的状态。
每次都会调用以下函数。所以你需要在这里设置选中或未选中状态的单元格属性。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.indexPath = indexPath //Create an indexPath property in your cell and set it here
//This will be required when you call the delegate method
//configure your cell state
return cell
}
您可以通过更改您已经调用的委托方法中的特定 属性 来更新 favoritePoets 数组中的模型,例如
favoritePoets[indexPath.row].selected = true or false
要访问 indexPath,您需要按照上面的代码进行设置。然后将其作为参数传递到您的委托方法中。现在这个更新的 属性 将帮助您在 cellForRowAt 函数中设置状态。
希望对您有所帮助:)。欢迎评论。