Armin
Armin

Reputation: 1281

UITableView Cell won't update after refresh (swift)

I have a tableview with cells that have certain buttons hidden and unhidden depending on the time, but when I refresh the tableview after a time change, nothing updates. I have to close the app (stop background) and reopen it for the change to take place.

Here's an edited version of my tableview's code that only includes details relevant to the issue:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = self.meTableView.dequeueReusableCellWithIdentifier("New Joined Party Cell", forIndexPath: NSIndexPath()) as! JoinedPartyCell

    var relationship = guestRelations[indexPath.row]
    var partyId = relationship.valueForKey("partyID") as! String
    var partyObject = PFObject(withoutDataWithClassName: "Party", objectId: partyId)
    var party:Party?
    partyObject.fetchIfNeededInBackgroundWithBlock({ (object, error) -> Void in

        party = Party(pfObject: object!)

        var currentDate = NSDate()
        var todayComponents = userCalendar.components(requestedDateComponents, fromDate: currentDate)

        var dateDifference = userCalendar.components(requestedDateComponents, fromDate: currentDate, toDate: party!.partyDate, options: nil)

        var monthString = ""
        var dayString = ""
        var hourString = ""
        var minuteString = ""

        if dateDifference.month != 0 {
            monthString = "\(dateDifference.month)mo "
        }
        if dateDifference.day != 0 {
            dayString = "\(dateDifference.day)d "
        }
        if dateDifference.hour != 0 {
            hourString = "\(dateDifference.hour)hr "
        }
        if dateDifference.minute != 0 {
            minuteString = "\(dateDifference.minute)min"
        }

        var countDownString = monthString + dayString + hourString + minuteString

        if party!.partyDate.earlierDate(self.today) == self.today {

            cell.guestListButton.hidden = false

            cell.guestListButton.setTitle("\(party!.guestAmount)/\(party!.guestLimit)", forState: UIControlState.Normal)
            cell.hereButton.enabled = true
            //cell.hereButton.backgroundColor = UIColor(hex: "#FD985A")

            cell.flexibilityLabel.hidden = false
            cell.uselessGuestLabel.hidden = false

            cell.guestStarButton.hidden = true
            cell.hereButton.hidden = true

            cell.timerView.hidden = false
            cell.timerLabel.text = countDownString

            cell.buttonCoverView.hidden = true

        } else if (party!.partyDate.earlierDate(self.today) == party!.partyDate && dateDifference.day == 0 && dateDifference.hour > -1 && dateDifference.hour <= 0) || party!.partyDate == NSDate() {

            cell.guestListButton.hidden = false

            cell.guestListButton.setTitle("\(party!.guestAmount)/\(party!.guestLimit)", forState: UIControlState.Normal)
            cell.hereButton.enabled = true
            //cell.hereButton.backgroundColor = UIColor(hex: "#FD985A")

            cell.flexibilityLabel.hidden = false
            cell.uselessGuestLabel.hidden = false

            cell.guestStarButton.hidden = true
            cell.hereButton.hidden = false

            cell.timerView.hidden = true

            cell.buttonCoverView.hidden = true

        } else {

            cell.guestListButton.hidden = true
            cell.hereButton.hidden = true
            cell.guestStarButton.hidden = false

            cell.flexibilityLabel.hidden = true
            cell.uselessGuestLabel.hidden = true

            cell.timerView.hidden = true

            cell.buttonCoverView.hidden = true

        }

    })

    cell.delegate = self

    return cell

}

Here's my code for refreshing:

func refresh(sender:AnyObject) {

    self.newGetParties()
    self.refreshControl?.endRefreshing()

}

UPDATE: For clarification, the newGetParties function executes self.meTableView.reloadData() at the end after the data is fetched.

Usually reloadData updates the cell, like it updates the timerLabel, but when it comes to certain views or buttons being hidden it won't change unless I close and reopen the app.

Upvotes: 0

Views: 1235

Answers (4)

Arshad Shaik
Arshad Shaik

Reputation: 1274

I got the similar issue as, i had few labels and background color, after going to a particular and come back its background color should change and the labels text should also change. But they were remaining same, until the viewdidload() method is called. The solution is very simple, in your custom tableviewcell class awakeFromNib() method set all the outlets to there default values as example below.

    leaveColorView.backgroundColor = UIColor.clear
    lblAppliedDuration.text = ""
    lblLeaveType.text = ""
    lblNoOfDays.text = ""
    lblLeaveReason.text = ""

The number of times the table reloads , it will clear the already existing values and replace with new one's.

I hope this helps someone.

Upvotes: 0

Gent
Gent

Reputation: 6425

Did you try to reload data in main thread?

dispatch_async(dispatch_get_main_queue(), {() -> Void in
    self.meTableView.reloadData()
})

Upvotes: 1

Etgar
Etgar

Reputation: 5924

Try removing the cell.delegate = self

Upvotes: 0

Mundi
Mundi

Reputation: 80271

If newGetParties() is on a background thread (e.g. if fetches data online) it will return before the data is updated. You should refresh your table after receiving and storing the new data.

Upvotes: 0

Related Questions