百恒网络
IOS开发之表视图的可重用对象
  • 百恒服务
  • APP开发
  • 网页案例
  • 网页知识
  • 关于我们
  • 联系我们
  • IOS开发之表视图的可重用对象

    2019-01-16 17:15:15 2815
           在iOS 6之后,表视图中有两种子视图采用可重用对象设计,它们是表视图单元格(UITableViewCell)和表视图节头节脚视图(UITableViewHeaderFooterView)。下面百恒科技小编就带大家一起来了解一下。

           1、表视图单元格
           表视图单元格的重用方法有两个:dequeueReusableCellWithIdentifier:方法和 dequeueReusableCellWithIdentifier:forIndexPath:方法。

           通过dequeueReusableCellWithIdentifier:方法,可以用标识符从表视图中获得可重用单元格,模式代码如下:
           let cellIdentifier = "CellIdentifier"
           var cell:UITableViewCell! =
           tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
            as? UITableViewCell
           if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Default,
            reuseIdentifier:cellIdentifier)
           }        

           static NSString *CellIdentifier = @"CellIdentifier";
           UITableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:CellIdentifier];
           if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
            } 

           要在表视图数据源的tableView:cellForRowAtIndexPath:方法中使用可重用单元格设计,首先通过
           dequeueReusableCellWithIdentifier:方法从表视图中找,如果cell为空,则需要使用initWithStyle:reuseIdentifier:构造器创建。 

           dequeueReusableCellWithIdentifier:forIndexPath:方法是iOS 6之后提供的方法。与上一个方法相比,该方法的签名多了forIndexPath:部分。它可以通过指定单元格位置获得可重用单元格,不需要判断,模式代码如下:
           let CellIdentifier = "CellIdentifier"
           var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier
            (CellIdentifier, forIndexPath:indexPath) as? UITableViewCell

           static NSString *CellIdentifier = @"CellIdentifier";
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
            CellIdentifier forIndexPath:indexPath]; 

           这个方法的使用有一些限制,它只能应用于iOS故事板中,并且在故事板中设计表视图单元格后,指定表视图单元格为动态的,Identifier属性设置为cellIdentifier。图1设定了表视图单元格的属性。

    IOS开发之表视图的可重用对象
    图1、表视图中的可重用单元格

           2、表视图节头节脚视图
           UITableViewHeaderFooterView也是iOS 6之后新加的内容,节头和节脚也会反复出现,它也需要可重用设计。

           使用表视图的dequeueReusableHeaderFooterViewWithIdentifier:方法获得UITableViewHeaderFooterView对象后,如果没有可重用的UITableViewHeaderFooterView对象,则使用initWithReuseIdentifier:构造器创建。其模式代码如下:
           override func tableView(tableView: UITableView, viewForHeaderInSection
            section:Int) -> UIView? {

            let headerReuseIdentifier = "TableViewSectionHeaderViewIdentifier"

            var sectionHeaderView :UITableViewHeaderFooterView!
            = tableView.dequeueReusableHeaderFooterViewWithIdentifier
            (headerReuseIdentifier) as? UITableViewHeaderFooterView

            if sectionHeaderView == nil {
            sectionHeaderView
            = UITableViewHeaderFooterView(reuseIdentifier:
            headerReuseIdentifier)
            }
            ......
            return sectionHeaderView
           }        

           - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
            (NSInteger)section
           {
            static NSString *headerReuseIdentifier =
            @"TableViewSectionHeaderViewIdentifier";
            UITableViewHeaderFooterView *sectionHeaderView = [tableView
            dequeueReusableHeaderFooterViewWithIdentifier:
            headerReuseIdentifier];
            if (!sectionHeaderView) {
            sectionHeaderView = [[UITableViewHeaderFooterView alloc]
            initWithReuseIdentifier:headerReuseIdentifier];
            }
            ......
            return sectionHeaderView;
           }

           需要在表视图委托协议UITableViewDelegate中的tableView:viewForHeaderInSection:方法中使用可重用对象设计。 

           关于IOS开发中表视图的可重用对象就先介绍到这里,了解更多关于南昌APP开发方面的知识,欢迎继续关注本公司网站!
    展开分享
    服务
    案例
    首页
    动态
    联系
    咨询