首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用reloadRowsAtIndexPaths或reloadSections导致表视图单元闪烁

使用reloadRowsAtIndexPaths或reloadSections导致表视图单元闪烁
EN

Stack Overflow用户
提问于 2015-10-09 02:55:43
回答 2查看 1.7K关注 0票数 0

我试图使用reloadRowsAtIndexPathsreloadSections来重新加载特定的tableView单元,当该单元格内的分段开关发生变化时(这反过来又改变了该单元格内UIScrollView中的显示)。使用此操作将导致最后一个tableView单元格(与重新加载的单元格无关)闪烁:单元格的背景会短暂地更改为黑色。最后一个tableView单元与另一个不同,因为它包含一个UICollectionView (见下图)。tableView中的所有单元格都是清晰的,backgroundView控制背景色梯度。

代码语言:javascript
运行
复制
//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单元的UICollectionView:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-15 05:06:44

我删除了该视觉效果视图,并通过向collectionView添加一个背景来实现相同的效果。闪烁的是视觉效果视图,而不是单元格或其中的其他视图。我通过编辑实现了与视觉效果视图相同的影响:

代码语言:javascript
运行
复制
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

包括:

代码语言:javascript
运行
复制
collectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.25)
票数 1
EN

Stack Overflow用户

发布于 2015-10-15 02:27:39

当更新UI以在主线程上调度和异步任务时,请记住:

代码语言:javascript
运行
复制
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 
    dispatch_async(dispatch_get_main_queue()) { 
        self.tableView.reloadData() 
    } 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33029150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档