[英]IOS Swift How can I change 1 table view cell
I have a tableview that has functionality on it to like and unlike a particular post. 我有一个tableview,它具有喜欢和不喜欢特定帖子的功能。 My issue is that if you like a post it shows correctly but if you scroll down and then up it forgets.
我的问题是,如果您喜欢某个帖子,它会正确显示,但是如果您向下滚动然后向上滚动,则会忘记。 How can I solve this?
我该如何解决?
@IBAction func voteAction(_ sender: UIButton)
{
let stream_id = streamsModel.streamID[sender.tag]
let profile_id = streamsModel.Profile_ID[sender.tag]
streamsModel.voteStatusv = streamsModel.VoteStatus[sender.tag]
streamsModel.Likes = streamsModel.Votes[sender.tag]
streamsModel.streamIDv = streamsModel.streamID[sender.tag]
let cellT = getCellForView(view: sender)
let indexPath = TableSource.indexPath(for: cellT!)
if (indexPath != nil) {
let cell = TableSource.cellForRow(at: indexPath!) as! HomeTVC
if streamsModel.likeStatus == "0" {
// Liked
cell.votes.setTitle(String(streamsModel.Likes+1), for: UIControlState.normal)
cell.likeImage.setImage(UIImage(named: "like2"), for: .normal)
} else {
// Unliked
cell.votes.setTitle(String(streamsModel.Likes-1), for: UIControlState.normal)
cell.likeImage.setImage(UIImage(named: "like"), for: .normal)
}
// This snippet below causes an error
//TableSource.reloadRows(at: [indexPath!], with: .none)
}
}
The code above simply checks if you liked something onClick if yes it adds +1 to the count and changes the image to like. 上面的代码只是检查您是否喜欢onClick,如果是,它将为计数增加+1并将图像更改为like。 If you unlike something then it does a -1 on the counter and changes the image to default.
如果您不喜欢某些东西,则它将在计数器上执行-1并将图像更改为默认图像。 Again everything works correctly.
同样,一切正常。 My only issue is that if I scroll down and then back up it forgets that I liked something.
我唯一的问题是,如果我向下滚动然后向上备份,则会忘记我喜欢的东西。 The only way I have of fixing this is to call
tableView.reloadData()
. 解决此问题的唯一方法是调用
tableView.reloadData()
。 However, that is too much since I'm only updating 1 cell. 但是,这太多了,因为我只更新1个单元格。 Any suggestions?
有什么建议么?
you need to also change value in your model array otherwise when you scroll tableview than you get old value.try this code 您还需要更改模型数组中的值,否则在滚动tableview时会获得旧值。请尝试此代码
if (indexPath != nil) {
let cell = TableSource.cellForRow(at: indexPath!) as! HomeTVC
if streamsModel.likeStatus == "0" {
// Liked
streamsModel.Likes = streamsModel.Votes[sender.tag] + 1
cell.votes.setTitle(String(streamsModel.Likes+1), for: UIControlState.normal)
cell.likeImage.setImage(UIImage(named: "like2"), for: .normal)
} else {
// Unliked
streamsModel.Likes = streamsModel.Votes[sender.tag] - 1
cell.votes.setTitle(String(streamsModel.Likes-1), for: UIControlState.normal)
cell.likeImage.setImage(UIImage(named: "like"), for: .normal)
}
// This snippet below causes an error
//TableSource.reloadRows(at: [indexPath!], with: .none)
}
You can use reloadRows(at:with:) method 您可以使用reloadRows(at:with :)方法
tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation)
NOTE: This methods accepts an array of IndexPaths, if you have to reload only a single cell, you need to pass that IndexPath in an array. 注意:此方法接受一个IndexPaths数组,如果您只需要重新加载单个单元格,则需要在该数组中传递该IndexPath。
Also, the intermittent like/unlike issue is very common in reusable cells. 同样,间歇性的/不相同的问题在可重复使用的单元格中很常见。 You need to maintain that selection separately to appropriately show like only on chosen cells.
您需要单独维护该选择,以仅在选定的单元格上适当显示。 If I get some more insight about what you are exactly doing, I may give you an exact solution.
如果我对您的工作有更多的了解,我可能会为您提供确切的解决方案。
Put this code in action of voteButton and then it reload particular cell. 将此代码放入投票按钮,然后重新加载特定的单元格。
let indexPath = IndexPath(item: sender.tag, section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.