简体   繁体   English

如何保存在单元格内按下按钮的动作,以便我可以添加图像而不会重复或消失

[英]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 ,它具有三个功能,其中两个运行成功。

  1. Add cells by pressing button inside of toolbar = works通过按工具栏内的按钮添加单元格 = 有效

  2. Being able to insert text into UITextView inside of cell, without the text duplicating and moving around = works能够将文本插入到单元格内的 UITextView 中,而无需文本复制和移动 = 有效

  3. 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.使用UITableViewUICollectionView时必须保留数据,因为它们在滚动时重用Cell so when you scroll the UITableView you are getting duplicate image or sometime loosing it.所以当你滚动 UITableView 时,你会得到重复的图像或有时会丢失它。 what you can do is :你可以做的是:

  1. Use an array of dictionary as dataSource to persist your data for the TableView.使用字典数组作为dataSource来保存 TableView 的数据。
  2. you can also create your own model and use as the dataSource of TableView .您还可以创建自己的模型并用作TableView
  3. and if your data is large enough then you can go for CoreData / Sqllight .如果您的数据足够大,那么您可以选择CoreData / Sqllight

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过按“添加”按钮添加新单元格并将文本保存到Coredata中 - How to add a new cell by press a Add button and save the text into Coredata 如何将长按手势识别器添加到tableview单元格内的按钮? - How to add long press gesture recognizer to a button inside of tableview cell? 快速按下带有文本字段中某些文本的按钮时,是否可以向表格视图添加新单元格? - Can I add a new cell to a tableview, when I press a button in swift, with some text from a textfield? 如何在自定义单元格中添加按钮? - How can I add a button in a custom cell? 触摸单元格时如何调用复选框按钮的动作? - How can I call the action of a checkbox button when the cell is touched? Swfit 3:如何通过按钮操作获取单元格中的所有项目? - Swfit 3 : How can I get all items in the cell with button action? 如何将操作按钮添加到自定义 XIB 单元格? - How to add an action button to a custom XIB cell? 如何为按钮添加遮罩以使隐藏区域不可点击? - How can I add a mask to a button so that the hidden area is not tappable? 我已显示该单元格,但我希望在按下按钮时将图像显示在集合视图单元格中 - I have the cell displayed but I want the image to be displayed in the collection view cell when i press the button 如何在 Swift 中首次按下按钮时添加触觉,而不是在释放按钮之后? - How can I add haptics on initial press of a button in Swift, not after the button has been released?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM