![](/img/trans.png)
[英]How to add a new cell by press a Add button and save the text into Coredata
[英]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
我有一個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
}
}
我期望cell的checkMarkImage
停留在按鈕被按下的cell,但實際效果是checkMarkImage
重新出現,滾動的時候,有時會完全消失
我擔心這樣做你不會達到預期的結果。 使用UITableView
或UICollectionView
時必須保留數據,因為它們在滾動時重用Cell
。 所以當你滾動 UITableView 時,你會得到重復的圖像或有時會丟失它。 你可以做的是:
dataSource
來保存 TableView 的數據。TableView
。CoreData
/ Sqllight
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.