Swift reuse cells in ios7.1 simulator, cells are hidden

一个人想着一个人 提交于 2019-11-30 09:54:15

问题


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
}

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()
       }
    }
}

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:

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

Edit2:
Feed View Controller:
http://pastebin.com/aqyxpimi
Custom Cell:
http://pastebin.com/4eMgkBc1


回答1:


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

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.

EDIT:

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.

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.



来源:https://stackoverflow.com/questions/27355545/swift-reuse-cells-in-ios7-1-simulator-cells-are-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!