简体   繁体   中英

UILabel Not Populated From Array When Button Pressed

I have a UITableViewController with custom UITableViewCells which contain a UILabel and a UIButton .

The problem is that the UILabel which is initially set to 0, is not changing when the button is pressed which changes its particular value in the array. When I use println() the actual array has been changed, but the UILabel has not.

The following is the code from my UITableViewController :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let groupChatCell = tableView.dequeueReusableCellWithIdentifier("groupChatCell", forIndexPath: indexPath) as textCell

    groupChatCell.voteScoreLabel.text="\(groupVote[indexPath.row])"
    groupChatCell.voteScoreLabel.tag=indexPath.row

    return groupChatCell
}

voteScoreLabel is the UILabel which I want to update from the array. groupVote is the array that is meant to populate the label with the scores. I set the UILabel tag equal to its index path so that I can then reference it when I need to pick out a particular value of the array.


The following is from my custom UITableViewCellController :

@IBAction func upVoteButtonPressed(sender: AnyObject) {
    groupVote[voteScoreLabel.tag]=groupVote[voteScoreLabel.tag]+1
}


What am I doing wrong here so that I can make the UILabel change when its corresponding value in the array has changed.

You need to reload the your tableView data when a button is clicked. The reloadData() function calls every tableView delegate/data source method again and reload the whole controller if its UITableViewController or the whole tableView if its a view added to another controller

Your code should look like this:

@IBAction func upVoteButtonPressed(sender: AnyObject) {
   groupVote[voteScoreLabel.tag]=groupVote[voteScoreLabel.tag]+1
   instance_of_your_tableView.reloadData()
   // If this is the name of method in swift //
}

Regards

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