Ios UITableViewCell图像在滚动时消失
我在UITableViewCell中有一个UIButton,点击按钮后图像会发生变化。尽管选定的按钮按预期被选中,但一旦UITableView滚动,选定的图像就会消失,因为单元格被重新使用 我写逻辑有困难。请帮忙 我的代码在下面,在Swift 3中 CellForRow:Ios UITableViewCell图像在滚动时消失,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我在UITableViewCell中有一个UIButton,点击按钮后图像会发生变化。尽管选定的按钮按预期被选中,但一旦UITableView滚动,选定的图像就会消失,因为单元格被重新使用 我写逻辑有困难。请帮忙 我的代码在下面,在Swift 3中 CellForRow: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//Button_Action
addSongButtonIdentifier(cell: cell, indexPath.row)
}
这是创建单元的位置:
func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int) {
let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! UIButton
//accessibilityIdentifier is used to identify a particular element which takes an input parameter of a string
//assigning the indexpath button
addButton.accessibilityIdentifier = String (index)
// print("visible Index:",index)
print("Index when scrolling :",addButton.accessibilityIdentifier!)
addButton.setImage(UIImage(named: "correct"), for: UIControlState.selected)
addButton.setImage(UIImage(named: "add_btn"), for: UIControlState.normal)
addButton.isSelected = false
addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside)
}
点击功能:
func tapFunction(sender: UIButton) {
print("IndexOfRow :",sender.accessibilityIdentifier!)
// if let seporated by a comma defines, if let inside a if let. So if the first fails it wont come to second if let
if let rowIndexString = sender.accessibilityIdentifier, let rowIndex = Int(rowIndexString) {
self.sateOfNewSongArray[rowIndex] = !self.sateOfNewSongArray[rowIndex]//toggle the state when tapped multiple times
}
sender.isSelected = !sender.isSelected //image toggle
print(" Array Data: ", self.sateOfNewSongArray)
selectedSongList.removeAll()
for (index, element) in self.sateOfNewSongArray.enumerated() {
if element{
print("true:", index)
selectedSongList.append(songDetailsArray[index])
print("selectedSongList :",selectedSongList)
}
}
}
您需要有一个数组来存储所选索引,就像您拥有的selectedSongList数组一样。然后,在cellForRow方法中,需要使用此数组中的bool proparty为按钮提供选中或取消选中的状态,或者在addSongButtonIdentifier方法中,需要设置选中状态
addButton.isSelected = selectedSongList.contains(indexPath.row)
最好的方法是使用模型类并跟踪单元格中的每个单独元素。但让我给你一个快速解决方案 创建一个自定义的Button类,如下面所示
类类名:UIButton{
var-imageName:String?
}
进入故事板,将类从UIButton更改为classname
在tableViewCellForIndexPath中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! classname
if let imgName = addButton.imageName {
addButton.setImage(UIImage(named: imgName), for: UIControlState.normal)
} else {
addButton.setImage(UIImage(named: "add_btn"), for:UIControlState.normal)
}
addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside)
return cell
}
现在让我们转到tapbutton实现
func tapFunction(发送方:类名){
func addSongButtonIdentifier(单元格:UITableViewCell,u索引:Int)
函数第addButton.isSelected=false行出现逻辑错误
它应该是addButton.isSelected=self.sateOfNewSongArray[index]
由于每次滚动表格时都会调用cellForRowAtIndexpath方法,因此它会重置“addButton”的选定状态。创建一个模型类以填充UITableView,并在该模型中获取UIImage变量,该模型将保存单元格的当前图像。单击按钮操作只需将UIImage变量更改为当前图像。不要多次问同一个问题。可能重复
print("IndexOfRow :",sender.accessibilityIdentifier!)
// if let seporated by a comma defines, if let inside a if let. So if the first fails it wont come to second if let
if let rowIndexString = sender.accessibilityIdentifier, let rowIndex = Int(rowIndexString) {
self.sateOfNewSongArray[rowIndex] = !self.sateOfNewSongArray[rowIndex]//toggle the state when tapped multiple times
}
sender.imageName = sender.imageName == "correct" ? "add_btn" : "correct" //image toggle
sender.setImage(UIImage(named: sender.imageName), for:UIControlState.normal)
print(" Array Data: ", self.sateOfNewSongArray)
selectedSongList.removeAll()
for (index, element) in self.sateOfNewSongArray.enumerated() {
if element{
print("true:", index)
selectedSongList.append(songDetailsArray[index])
print("selectedSongList :",selectedSongList)
}
}
}