简体   繁体   中英

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. 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. If you unlike something then it does a -1 on the counter and changes the image to default. 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() . However, that is too much since I'm only updating 1 cell. 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

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM