简体   繁体   English

UITableView单元格重复

[英]UITableView Cell repeating

I created a tableview with a custom cell showing data. 我创建了一个带有显示数据的自定义单元格的表格视图。 The display works fine. 显示效果很好。 But the cells are repeating so there are more cells being displayed then items in the array. 但是单元格在重复,因此要显示的单元格要多于数组中的项。

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    // when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
        return self.AEDItems.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.AEDItems.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

        // Check if controller is active. If user is doing a search retrieve the aeds from the search result rather than the AED array.
        let aed = self.AEDItems[indexPath.row]
        cell.addressLabel?.text = aed.owner
        cell.objectLabel?.text = aed.street

        return cell
    }

Why is this happening? 为什么会这样呢? I guess it has something to do with the cell reuseage. 我猜想这与单元重用有关。 But does the count of items does not solve it? 但是,项目数不能解决问题吗?

The problem is this method: 问题是这种方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return self.AEDItems.count
}

This method is asking for the number of sections. 该方法要求提供节数。 You should return simply 1 from this method. 您应该仅从此方法返回1

Instead, you have as many sections as you do objects in your AEDItems array. 相反,在AEDItems数组中,您拥有与对象一样多的部分。 And then in your other methods, you're not putting any section specific logic. 然后,在其他方法中,您无需放置任何特定于部分的逻辑。 So every section is identical to each other. 因此,每个部分都相同。 But we actually only want one section with many rows. 但是我们实际上只希望一个包含很多行的部分。

So, we actually want simply: 因此,我们实际上只想:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1
}

Or, as @johnpatrickmorgan clarifies in the comments, we can omit this optional protocol method entirely, as UITableView assumes only a single section if its datasource has omitted this method. 或者,正如@johnpatrickmorgan在评论中阐明的那样,我们可以完全省略此可选协议方法,因为如果UITableViewdatasource省略了此方法,则它仅假定一个部分。

For more reading, check out the UITableView Data Source protocol documentation regarding numberOfSectionsInTableView: . 有关更多信息,请查看有关numberOfSectionsInTableView:UITableView数据源协议文档

You need to have only section. 您只需要有一节。 If it is one then Make it one only. 如果是一个,则仅设置为一个。 If you have multiple sections and data for each section then use that data for each section 如果您有多个部分,并且每个部分都有数据,那么请为每个部分使用该数据

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1 
    //return self.AEDItems.count
}

To experience Section, use below methods of UITableView 要体验本节,请使用以下UITableView方法

func tableView( tableView : UITableView,  titleForHeaderInSection section: Int)->String {

return "Section"
}

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
{
    return 20.0
}

I would suggest you to check this tutorial Customizing Header and Footer of Table View in iOS8 with Swift 我建议您检查一下本教程,使用Swift在iOS8中自定义表视图的页眉和页脚

You are returning array's count for section that's why it is happening so please return 1 section . 您要返回的是部分的数组计数,这就是发生这种情况的原因,因此请返回1 section

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    // when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
        return 1
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM