wxcoder
wxcoder

Reputation: 655

Using reloadRowsAtIndexPaths or reloadSections cause Tableview Cell to flicker

I am trying to use either reloadRowsAtIndexPaths or reloadSections to reload a particular tableView cell when a segmented switch within that cell has changed (which in turn changed the display in the UIScrollView within that cell). Using this causes the last tableView cell (not related to the reloaded cell) to flicker: the background of the cell changes to a black color briefly. This last tableView cell is different from the other, as it is contains a UICollectionView (see the picture below). All cells in the tableView are clear, with the backgroundView governing the background color gradient.

//tableView cellForRowAtIndexPath 
case .cellToReload:
            let cell = tableView.dequeueReusableCellWithIdentifier("graphs", forIndexPath: indexPath) as! WeatherTableViewCell
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            let segmentControl = cell.viewWithTag(1) as! UISegmentedControl
            segmentControl.selectedSegmentIndex = segment
            segmentControl.addTarget(self, action: "segmentAction:", forControlEvents: .ValueChanged)
            cell.graph.delegate = self
            cell.graph.showsHorizontalScrollIndicator = false
            if segment == 0 || segment == 1 {
                cell.graph.contentSize.width = cell.frame.width * 2.0
            } else {
                cell.graph.contentSize.width = cell.frame.width
            }
            cell.graph.contentSize.height = cell.graph.frame.height
            let ConditionsView = UIView(frame: CGRectMake(20, 0, cell.graph.contentSize.width, cell.graph.contentSize.height))
            cell.backgroundColor = UIColor.clearColor()
        switch (segment) {
            //create new view and add as a subview to cell.graph

case .flickeringCell:
    let cell = tableView.dequeueReusableCellWithIdentifier("weeklyWeatherCell", forIndexPath: indexPath) as! WeatherTableViewCell
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.forecastCollectionView.delegate = self
    cell.forecastCollectionView.dataSource = self
    cell.forecastCollectionView.reloadData()
    cell.forecastCollectionView.tag = indexPath.row
    cell.forecastCollectionView.showsHorizontalScrollIndicator = false
    cell.backgroundColor = UIColor.clearColor()
    cell.forecastCollectionView.backgroundView = cell.backgroundView
    cell.forecastCollectionView.backgroundColor = cell.backgroundColor
    return cell


func segmentAction(sender: UISegmentedControl) {
    let path = NSIndexSet(index: 1)
    switch sender.selectedSegmentIndex {
    case 0:
        segment = 0
        tableView.reloadSections(path, withRowAnimation: .Fade)
    case 1:
        segment = 1
        tableView.reloadSections(path, withRowAnimation: .Fade)
    case 2:
        segment = 2
        tableView.reloadSections(path, withRowAnimation: .Fade)
    default:
        break
    }
    
}

//Table background
func createGradientForTable {
    let gradientLayer = CAGradientLayer()
    ...
    gradientLayer.startPoint = CGPoint(x: 0, y: 0)
    gradientLayer.endPoint = CGPoint(x: 0, y: 1)
    let backgroundView = UIView(frame: sender.tableView.frame)
    backgroundView.layer.insertSublayer(gradientLayer, atIndex: 0)
    sender.tableView.backgroundView = backgroundView
}

TableView cell with the UICollectionView:

enter image description here

Upvotes: 0

Views: 1861

Answers (2)

wxcoder
wxcoder

Reputation: 655

I removed that Visual Effect View and implement the same effect by adding a background to the collectionView. It was the Visual Effect View that was flickering and not the cell or the other views within it. I achieved the same affect as the Visual Effect View by editing:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

to include:

collectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.25)

Upvotes: 1

Tr0yJ
Tr0yJ

Reputation: 3280

Remember when updating UI to dispatch and async task on the main thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 
    dispatch_async(dispatch_get_main_queue()) { 
        self.tableView.reloadData() 
    } 
}

Upvotes: 0

Related Questions