如何刷新表视图中的特定单元格并存储更改的数据以便稍后在同一表视图中显示

How to Refresh particular cell in tableview and store that changed data to show later on same tableview

我已经实现了带有分页功能的表视图。当我点击按钮操作时,我想用类似的图像更新单元格图像,如果我没有刷新整个表格视图如何保存更新的特定表格视图数据。当我再次滚动回到那个单元格时。

let indexPathRow:Int = self.toreloadindexint 
let indexPosition = IndexPath(row: indexPathRow, section: 0)                                           
self.tablall.reloadRows(at: [indexPosition], with: .fade)
//......tableview cell data for reload data that i get from api..........
       if updateintoflike == 0
        {
            // print("after updation on cell count",self.toreloadindexint)
            var favcounts = arrayfeedwebcounts[indexPath.row] as! Int //"data"
            lblfav?.text = "0"//String(favcounts)
            favcounts = 0
            print("fav..", lblfav?.text)
        }
        else
        {
           // print("after updation on cell count",self.toreloadindexint)
            lblfav?.text = ""

            var row: Int = indexPath.row
            arrayfeedwebcounts.remove(at: row)
            arrayfeedwebcounts.insert(updateintoflike as AnyObject, at:row)
            var addedcontt = arrayfeedwebcounts[indexPath.row] as! NSNumber
            lblfav?.text = String(updateintoflike)
            print("label fav..", lblfav?.text)
       }

解决方案 1:-

let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)

如果不行就试试这个

 self.tableView.beginUpdates()
 let indexPath = NSIndexPath.init(row:  self.selectedTag, section: 0)     
 self.tableView.reloadRows(at: [indexPath], with: .automatic)
 self.tableView.endUpdates()

如果它不起作用,请发表评论,我也检查一下。

您需要在 tableView's dataSource 模型中维护 UITableViewCell 的状态。我将尝试通过示例详细说明。

1. 假设您 model 看起来像:

class Post {
    var isLiked = false
    var likeCount = 0
}

2. 接下来,您需要创建自定义 UITableViewCell 以便它根据button 动作,即

class CustomCell: UITableViewCell {
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var likeButton: UIButton!

    var post: Post?

    func configure(with post: Post) {
        countLabel.text = "\(post)"
        likeButton.isSelected = post.isLiked
    }

    @IBAction func likeButtonTapped(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if let post = post {
            post.isLiked = sender.isSelected
            if sender.isSelected {
                post.likeCount += 1
            } else {
                post.likeCount -= 1
            }
            countLabel.text = "\(post.likeCount)"
        }
    }
}

3. 最后,UITableViewDataSource 方法将是,

class VC: UIViewController, UITableViewDataSource {
    var posts = [Post]()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.configure(with: posts[indexPath.row])
        return cell
    }
}

在上面的代码中,只要按下 likeButtonUIdataSource 就会更新。因此,每当 cell 再次显示时,您将自动看到最后更新的数据。

无需在每次点击 likeButton 时重新加载 tableViewcell。重新加载只会导致额外的开销。