我有一个tableViewController
,它有三个函数,其中两个函数运行成功。
-
通过按下工具栏内的按钮添加单元格=工作
-
能够将文本插入单元格内的UITextView中,而无需复制和移动文本=工作
-
当按下单元格中的按钮时,会出现checkMarkImage,滚动和移动时不会重复=不起作用
checkMarkImage
不会停留在按下按钮的单元格中。它在滚动时会重新出现,有时会消失,尽管我努力使用布尔值来跟踪选中的单元格。
我的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)
}
}
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
}
}
我预计单元格的checkMarkImage
会停留在按下按钮的单元格中,但实际效果是checkMarkImage
在滚动时会重新出现,有时会完全消失
我担心这样做不会达到预期的结果。当您使用UITableView
或UICollectionView
时,您必须保留您的数据,因为它们在滚动时会重用Cell
。所以当你滚动UITableView时,你会得到重复的图像,或者有时会丢失它。你可以做的是:
- 使用字典数组作为
dataSource
为TableView持久化数据 - 您也可以创建自己的模型并用作
TableView
的dataSource - 如果你的数据足够大,那么你可以选择
CoreData
/Sqllight