[英]how to save the action of a button press inside of a cell, so that I can add an image without it duplicating or disappearing
I have a tableViewController
that has three functions, two of which are working successfully.我有一个
tableViewController
,它具有三个功能,其中两个运行成功。
Add cells by pressing button inside of toolbar = works通过按工具栏内的按钮添加单元格 = 有效
Being able to insert text into UITextView inside of cell, without the text duplicating and moving around = works能够将文本插入到单元格内的 UITextView 中,而无需文本复制和移动 = 有效
When button in cell is pressed, checkMarkImage appears, without getting duplicated when scrolling and moving around = does not work按下单元格中的按钮时,会出现 checkMarkImage,滚动和移动时不会被复制 = 不起作用
The checkMarkImage
does not stay put at the cell in which the button is pressed. checkMarkImage
不会留在按下按钮的单元格中。 It reappears when scrolling and sometimes disappears, despite my efforts of using a Boolean value, in order to track the cells of which have been checkmarked.滚动时它会重新出现,有时会消失,尽管我努力使用布尔值来跟踪已被选中的单元格。
the configuration of the button inside of the my customCellClass:我的 customCellClass 内部按钮的配置:
@IBAction func checkMarkButton(_ sender: UIButton) {
if checkedOff{
UIImageView.animate(withDuration: 0.3, delay: 0, options: [], animations: {
self.checkMarkImage.alpha = 0.0
self.notesTextView.isEditable = true
self.notesTextView.textColor = .white
self.checkedOff = false
},completion: nil)
}
else{
UIImageView.animate(withDuration: 0.3, delay: 0, options: [], animations: {
self.checkMarkImage.alpha = 1.0
self.notesTextView.isEditable = false
self.notesTextView.textColor = .orange
self.checkedOff = true
}, completion: nil)
}
}
The handling of cell inside cellForRowAt
: cellForRowAt
中单元格的处理:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "TableViewNotesCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewNotesCell
cell.notesTextView.text = cellNumber[indexPath.row]
if cellNumber[indexPath.row] < " "{
print("celltext is less than nothing")
cell.notesTextView.textColor = .white
cell.notesTextView.isEditable = true
cell.checkMarkImage.alpha = 0.0
}
else{
if cell.checkedOff{
print("cell has been checked off")
cell.notesTextView.textColor = .orange
cell.notesTextView.isEditable = false
cell.checkMarkImage.alpha = 1.0
}
}
My expect the cell's checkMarkImage
to stay at the cell in which the button is pressed, but the actual effect is that the checkMarkImage
is re-occurring, when scrolling, and sometimes completely disappears我期望cell的
checkMarkImage
停留在按钮被按下的cell,但实际效果是checkMarkImage
重新出现,滚动的时候,有时会完全消失
I am afraid that by doing this you not gonna achieve the desired outcome.我担心这样做你不会达到预期的结果。 you must persist your data when you using
UITableView
or UICollectionView
because these reuse the Cell
when scrolling.使用
UITableView
或UICollectionView
时必须保留数据,因为它们在滚动时重用Cell
。 so when you scroll the UITableView you are getting duplicate image or sometime loosing it.所以当你滚动 UITableView 时,你会得到重复的图像或有时会丢失它。 what you can do is :
你可以做的是:
dataSource
to persist your data for the TableView.dataSource
来保存 TableView 的数据。TableView
.TableView
。CoreData
/ Sqllight
.CoreData
/ Sqllight
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.