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

时间:2019-06-21 09:46:04

标签: swift uitableview swift4

我已经使用分页功能实现了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)
       }

2 个答案:

答案 0 :(得分:0)

解决方案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()

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

答案 1 :(得分:0)

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

1。。假设您model看起来像:

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

2。。接下来,您需要创建自定义UITableViewCell,以便根据dataSource修改UIbutton 动作,即

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
    }
}

在上面的代码中,每当按下likeButton时,UIdataSource都会被更新。因此,每当再次显示cell时,您都会自动看到最近更新的数据。

每次点击tableView时都不需要重新加载celllikeButton。重新加载只会导致额外的开销。