简体   繁体   中英

Flickering Section Headers when it refreshes?

I have aa series of UITableViewCells that update every second or so.

First Attempt: In order to perform such an update, I call self.tableView.reloadData() to reload all the cell contents.

Second Attempt: I have tried just refreshing the sections with a UITableViewAnimation.None but that makes the section header disappear and then reappear every second.

In the first attempt, I get a "flickering" effect because the views are constantly being refreshed. I want it so that the flickering effect goes away. In the second attempt, as said above, the section header disappear and then reappear every second.

In my code, I want to constantly update the headerView.headerTimeLeft.text label.

Here are my attempts:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.refreshTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "refreshView:", userInfo: nil, repeats: true)
}

func refreshView(timer: NSTimer){
    //Attempt 2
    var visibleRows:NSArray = self.tableView.indexPathsForVisibleRows()!
    var sections = NSMutableIndexSet()
    for indexPath in visibleRows{
        sections.addIndex(indexPath.section)
    }

    //Attempt 1 was simply to reload the Data
    //self.tableView.reloadData()
    //Attempt 2 was to reload the sections
    self.tableView.reloadSections(sections, withRowAnimation: UITableViewRowAnimation.None)
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //Show section header cell with name of event creator
    var cellIdentifier = "SectionHeaderCell"
    var headerView = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! SectionHeaderCell

    headerView.backgroundColor = UIColor.whiteColor()
    headerView.creator.text = self.events[section].eventCreator()

    var fromDate = self.events[section].fromDate()! + " " + self.events[section].fromTime()!

    var dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = "MM/dd/yy hh:mm a z"
    var eventStartDate = dateFormatter.dateFromString(fromDate)?.timeIntervalSince1970

    var timeUntilEnd = eventStartDate! - NSDate().timeIntervalSince1970

    //Display the time left
    var seconds = timeUntilEnd % 60
    var minutes = (timeUntilEnd / 60) % 60
    var hours = timeUntilEnd / 3600
    headerView.headerTimeLeft.text = NSString(format: "%dh %dm %ds", Int(hours), Int(minutes), Int(seconds)) as String
    return headerView
}

You're just trying to update the label correct? You shouldn't need to redraw the entire header view. If you just change the label text and possibly call setNeedsDisplay on the label it should take care of itself.

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