在ios7.1模拟器中快速重用单元格,隐藏单元格 [英] Swift reuse cells in ios7.1 simulator, cells are hidden

查看:159
本文介绍了在ios7.1模拟器中快速重用单元格,隐藏单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这完全适用于iOS8.1模拟器。原始代码:

This works on iOS8.1 simulator perfectly. Original code:

func updateCell(path:Int){
    let indexPath = NSIndexPath(forRow: path, inSection: 0)
    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
    tableView.endUpdates()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:PhotoCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PhotoCell

    if data[indexPath.row].objectId == ""{
        getData.getData(universityData.tableName, sender: self, cellId: indexPath.row)
    }else{
        var subviews = cell.subviews
        for view in subviews{
            view.removeFromSuperview()
        }
    }
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width;
    tableView.rowHeight = screenWidth + 38
    //Other cell stuff here 
    return cell
}

但是在iOS7.1模拟器上,
1.所有单元格在顶部单元格顶部堆叠起来,而不是在第二个
2中向下滑动。当单元格首次加载时,它们看起来正常
3.在代码中调用else部分的第二个加载单元格开始消失而不是重新加载

But on iOS7.1 simulator, 1. All the cells start as stacked together on top of the top cell, than slide down in a second 2. When the cells first load they look normal 3. On the 2nd load which calls the "else" part in the code the cells start disappearing instead of reloading

我找到了这样的答案并尝试以这种方式更改代码:

I found an answer like this and tried to change the code this way:

else{  
    for view in cell.contentView.subviews{
       if view.isKindOfClass(UIView){
            view.removeFromSuperview()
       }
    }
}

案例1和2仍在发生。但是细胞在两个图像之间切换,首先是正常的清洁细胞,第二个是旧数据仍然在里面的细胞:

Case 1&2 are still happening. But the cells toggle between the 2 images, first a normal clean cell, second a cell with old data still inside:

好的问题是,如何在iOS 7.1和iOS上同时加载单元格8.1?

Ok so the question is, how to have the cells load consistently both on iOS 7.1 & 8.1?

编辑2:

Feed视图控制器:

http://pastebin.com/aqyxpimi

自定义单元格:

http://pastebin.com/4eMgkBc1

推荐答案

每次表视图使用以下代码请求单元格时,您都在更新tableview属性:

You are updating your tableview properties every time the table view requests a cell with this code:

tableView.separatorStyle = UITableViewCellSeparatorStyle.None
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width;
tableView.rowHeight = screenWidth + 38

这是不必要的,可能会导致问题。你的行高度没有被设置,直到它请求我怀疑导致行高在iOS 7上无法正常工作的第一个单元格。相反,你应该只在 viewDidLoad 上执行此操作在视图控制器中。

This is unnecessary and potentially causing problems. Your row height is not being set until it requests the first cell which I suspect is causing the row height to not work properly on iOS 7. Instead you should do this only on viewDidLoad within your view controller.

此外,没有理由你应该像这样任意删除单元格的所有子视图。您不仅要从直接单元格中删除视图,还要在每次表视图重用时删除它。这意味着在重用单元格时将没有显示数据的视图(如果它被重用于具有数据的行)。相反,您应该填写空数据或默认数据。

Also, there is no reason you should be removing all subviews of a cell arbitrarily like that. You are not only removing the views from the immediate cell, but you are removing it for every time it is reused by the table view. That means there will be no views to display the data when the cell is reused (if it is reused for a row that does have data). Instead, you should either fill in empty data or default data.

编辑:

查看完整代码后,不应在数据源方法中向子单元添加子视图。每次重复使用单元格时,都会向其中添加其他视图,从而留下旧视图。我看到你试图删除旧视图,但是你不加区别地删除了所有子视图,这是一个非常糟糕的主意。

After looking at your full code, you should not be adding subviews to your cells in the data source method. Every time your cell is reused, you are adding additional views to it, leaving old views around. I see you tried to remove old views but your are indiscriminately removing all subviews which is a really bad idea.

相反,你应该将单元格添加到带有出口的笔尖您的单元类,或者您应该在您的单元类中以编程方式添加它们。您可能应该在 init(样式:UITableViewCellStyle,reuseIdentifier:String?) awakeFromNib 中执行此操作但您可以如果您知道您的细胞总是来自笔尖,那么只需选择一个。数据源方法应该只配置自定义视图,而不是创建,添加或删除它们。

Instead, you should either add the cells to the nib with outlets to your cell class, or you should be adding them programmatically in your cell class. You should probably be doing this in both init(style: UITableViewCellStyle, reuseIdentifier: String?) and awakeFromNib but you can just choose one if you know your cells will always either be from a nib or not. The data source method should only be configuring your custom views, not creating, adding, or removing them.

这篇关于在ios7.1模拟器中快速重用单元格,隐藏单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆