刷新后,UITableView Cell不会更新(快速)

时间:2015-08-23 16:00:42

标签: ios swift uitableview nsdate reloaddata

我有一个tableview,其中的单元格根据时间隐藏和取消隐藏某些按钮,但是当我在时间更改后刷新tableview时,没有任何更新。我必须关闭应用程序(停止背景)并重新打开它以进行更改。

这是我的tableview代码的编辑版本,仅包含与问题相关的详细信息:

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

}

这是我刷新的代码:

func refresh(sender:AnyObject) {

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

}

更新:为了澄清,newGetParties函数在获取数据后在最后执行self.meTableView.reloadData()。

通常reloadData更新单元格,就像更新timerLabel一样,但是当隐藏某些视图或按钮时,除非我关闭并重新打开应用程序,否则它不会发生变化。

4 个答案:

答案 0 :(得分:1)

您是否尝试在主线程中重新加载数据?

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

答案 1 :(得分:0)

如果newGetParties()位于后台线程上(例如,如果在线获取数据),它将在数据更新之前返回。 接收并存储新数据后,您应该刷新表

答案 2 :(得分:0)

尝试删除cell.delegate = self

答案 3 :(得分:0)

我得到了类似的问题,我有很少的标签和背景颜色,去了一个特定的,然后回来它的背景颜色应该改变,标签文本也应该改变。但它们保持不变,直到调用viewdidload()方法。解决方案非常简单,在您的自定义tableviewcell类awakeFromNib()方法中,将所有出口设置为默认值,如下所示。

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

表重新加载的次数,它将清除已存在的值并替换为新的值。

我希望这有助于某人。