Swift 如何刷新tableview中的特定单元格并存储更改后的数据,以便稍后在同一tableview上显示
我已经用分页工具实现了tableview。当我点击按钮操作时,我想用like image更新单元格图像,如果我没有刷新整个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)
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
}
}