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

后端 未结 1 449
一整个雨季
一整个雨季 2020-12-22 05:53

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

func updateCell(path:Int){
    let indexPath = NSIndexPath(forRow: path, inSection: 0)
    tableView         


        
相关标签:
1条回答
  • 2020-12-22 06:13

    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.

    0 讨论(0)
提交回复
热议问题