网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

UIKit框架(21)UITableView实现复杂单元格(二)-创新互联

继续介绍第二种实现复杂单元格的方式 ---- 使用storyboard中的prototype cell

10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有渝水免费网站建设让你可以放心的选择与我们合作。
  • prototype Cell介绍

在storyboard或xib中使用tableView时,可以添加prototype cells,其好处是:

1)可以直接进行自定义cell的设置

2)通过设置cell的重用ID,在调用出队列方法时如果没有可重用的则创建一个新的

   UIKit框架(21)UITableView实现复杂单元格(二)

注:一个tableView中可以设计多个prototype cell,重用ID不同即可

使用prototype cell 设计tableView是最常用的一种方式!

使用prototype cell相比较于纯代码,可以简化两处编程

  • 简化cell获取的代码

使用了prototype cell之后,从tableView的重用队列获取指定ID的cell,如果获取不出来,会自动创建一个相同ID的prototype cell的副本,不再需要alloc+init进行创建

//1.1 实现类方法,获取cell
+ (AMHeroCell *)cellWithTableView:(UITableView *)tableView
{
    AMHeroCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

  • 简化cell的子视图设定

使用storyboard设计cell内部的子视图,也就是说不需要在代码中创建子视图并进行自动布局,这些工作全部可以在storyboard中快速完成。

也就是说,上篇文章中,介绍的1.2 1.3这两步不再需要

  • 案例:模拟实现大众点评的首页

prototype cell是我们实现复杂tableView的一种快速开发手段

如实现如下截图的效果:

    UIKit框架(21)UITableView实现复杂单元格(二)

可以看到:

    三个section,每个section中的cell样式不同及个数不同

    第一个section:一个cell,scrollView,内部有很多按钮

    第二个section:两个cell,活动信息cell

    第三个section:很多个cell,产品信息cell

1)在storyboard设计出这三种cell

    UIKit框架(21)UITableView实现复杂单元格(二)

    每种cell设定不同的ID

3)创建每个cell需要显示的数据模型类

4)每种cell分别关联UITableViewCell的子类

    子类内部分别实现三步操作(见UIKit框架(21)UITableView实现复杂单元格(一))

5)控制器管理所有的数据模型并实现数据源、代理方法

#pragma mark - tableView 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 ) {
        return 1;
    }
    else if ( section == 1 ) {
        return self.activityArray.count;
    }
    else if ( section == 2 ) {
        return self.goodsArray.count;
    }
    else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.section == 0 ) {
        return [AMFunctionSelectionCell cellWithTableView:tableView];
    }
    else if ( indexPath.section == 1 ) {
        return [AMActivityCell cellWithTableView:tableView];
    }
    else if ( indexPath.section == 2 ) {
        return [AMGoodsInfoCell cellWithTableView:tableView];
    }
    else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.section == 0 ) {
        return [AMFunctionSelectionCell cellHeight];
    }
    else if ( indexPath.section == 1 ) {
        return [AMActivityCell cellHeight];
    }
    else if ( indexPath.section == 2 ) {
        return [AMGoodsInfoCell cellHeight];
    }
    else {
        return 0;
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.section == 0 ) {
        
    }
    else if ( indexPath.section == 1) {
        ((AMActivityCell*)cell).activityModel = self.activityArray[indexPath.row];
    }
    else if ( indexPath.section == 2 ) {
        ((AMGoodsInfoCell*)cell).goodsInfoModel = self.goodsArray[indexPath.row];
    }
}

  • 案例扩展:加载更多

在最后一个section下面增加一个加载更多按钮

    UIKit框架(21)UITableView实现复杂单元格(二)

点击后,可以模拟一个简单的增加cell的操作

1)使用xib设计一个这个底部视图

2)关联一个UIView的子类并提供类方法快速创建

+ (AMGoodsInfoFooterView *)view
{
    return [[[NSBundle mainBundle] loadNibNamed:@"AMGoodinfoFooterView" owner:nil options:nil]lastObject];
}

3)控制器实现tableView的数据源方法

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if ( section == 2 ) {
        AMGoodsInfoFooterView * v =  [AMGoodsInfoFooterView view];
        v.delegate = self;
        return v;
    }
    else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ( section == 2 ) {
        return 40;
    }
    else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

    说明:两处返回5的目的是让三个section之间的空隙不要太大

4)加载更多按钮被点击时,将点击产生这个时间通过代理传递给控制器

//UIView子类提出代理协议并添加代理属性
@protocol AMGoodsInfoFooterViewDelegate 
@optional
- (void) footerViewLoadMoreGoods;
@end
@interface AMGoodsInfoFooterView : UIView
+ (AMGoodsInfoFooterView *) view;
@property (nonatomic, weak) id delegate;
@end

5)控制器成为代理并实现代理方法

成为代理的操作在3)中已有

代理方法的实现:(简单模拟)

- (void)footerViewLoadMoreGoods
{
    AMGoodsInfoModel * model = [[AMGoodsInfoModel alloc] init];
    model.icon = self.goodsArray[0].icon;
    model.goodTitle = @"东软食堂";
    model.goodFooter = @"已售999999999";
    model.goodDesc = @"吃了你就知道了";
    model.priceValue = @"8";
    [self.goodsArray addObject:model];
    
    NSIndexSet * s = [NSIndexSet indexSetWithIndex:2];
    [self.tableView reloadSections:s withRowAnimation:UITableViewRowAnimationLeft];
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:UIKit框架(21)UITableView实现复杂单元格(二)-创新互联
新闻来源:http://bjjierui.cn/article/ecdih.html

其他资讯