简体   繁体   English

iOS Swift如何更改1个表格视图单元格

[英]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. 注意:此方法接受一个I​​ndexPaths数组,如果您只需要重新加载单个单元格,则需要在该数组中传递该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.

相关问题 如何在iOS表格视图的底部制作固定单元格? - How can I make the fixed cell at the bottom of the table view in iOS? 我如何编辑表格视图节标题iOS Swift - how can i edit a table view section header ios swift Swift-如何更改修改此表格视图的框架? - Swift - How can i change revise the frame of this table view? 如何在 iOS 中更改内部表格视图单元格的单元格高度? - How to change the cell height of inner table view cell in iOS? 如何在Swift iOS中更改自定义“收藏夹视图”单元格的背景颜色? - How to change the background color of a custom Collection View cell in swift iOS? iOS如何在表格视图单元格解析中编辑(更新)日期 - iOS How to edit(update) date i table view cell parse Swift 如何在iOS中将表格视图中的表格单元格中的图像解析为详细信息视图? - How can I parse image from table cell inside table view to detail view in iOS? 如何将多个图像添加到Swift iOS中的自定义表格视图单元格? - How to add multiple images to to custom table view cell in swift ios? iOS如何从表视图单元格Swift中的解析中下载图像 - iOS How to download image from parse in table view cell Swift iOS如何在表格视图单元格Swift中调整图像大小 - iOS How to resize image at table view cell Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM