Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何刷新tableview中的特定单元格并存储更改后的数据,以便稍后在同一tableview上显示_Swift_Uitableview_Swift4 - Fatal编程技术网

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

Swift 如何刷新tableview中的特定单元格并存储更改后的数据,以便稍后在同一tableview上显示,swift,uitableview,swift4,Swift,Uitableview,Swift4,我已经用分页工具实现了tableview。当我点击按钮操作时,我想用like image更新单元格图像,如果我没有刷新整个tableview,如何保存更新后的特定tableview数据。当我再次滚动回该单元格时 let indexPathRow:Int = self.toreloadindexint let indexPosition = IndexPath(row: indexPathRow, section: 0)

我已经用分页工具实现了tableview。当我点击按钮操作时,我想用like image更新单元格图像,如果我没有刷新整个tableview,如何保存更新后的特定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的
数据源
模型中维护
UITableViewCell
的状态。我将通过一个例子来阐述这一点

1.让我们假设您的
model
看起来像:

class Post {
    var isLiked = false
    var likeCount = 0
}
2.接下来,您需要创建自定义
UITableViewCell
,以便它根据
按钮
操作修改
数据源
UI
,即

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
    }
}
在上面的代码中,每当按下像按钮一样的
时,
UI
dataSource
都会更新。因此,无论何时再次显示
单元格
,您都会自动看到上次更新的数据


每次点击像按钮一样的
按钮时,无需重新加载
表格视图
单元格
。重新加载只会导致额外的开销。

我为刷新行添加了此代码,但刷新后显示错误的图像和数据,您需要在数据源中保持类似的状态。是的,这是问题,我在标签中得到了错误的计数,甚至我通过单元格中的新变量检查了刷新状态,该变量显示了错误的值@PGDevAdd更多代码,以便我可以调试。我做了。我的tableview单元格中有代码。它也有什么问题吗?让row:Int=indexPath.row arrayfeedwebcounts.remove(at:row)arrayfeedwebcounts.insert(updateintoflike as AnyObject,at:row)var adddcontt=arrayfeedwebcounts[indexPath.row]as!NSNumber lblfav?.text=String(updateintoflike)print(“label fav..”,lblfav?.text)我的代码用于重新加载tableview的特定行,您的代码是insert和delete Row,对吗?好的,我认为您的代码是正确的。您能详细说明问题以便我能帮助您吗,我找到了可以解决错误数据问题的解决方案。谢谢您的模型结构。@PGDev
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
    }
}