您的位置:首页 > 产品设计 > UI/UE

IOS之UITableView

2016-07-06 21:59 513 查看
概述:

Table可能是最擅长于展示数据的一种UI部件。因此UITableView这个类是iOS App开发中除button和Label之外最常用的控件类型。几乎任何一个App都离不开tableView。使用UITableView很简单,核心就是要实现两个protocol。这是由于tableView在MVC中只是V这一环,所以它需要额外的支持提供另外的MC两个环节的功能才能让一个table完整的工作。而这种支持实现的途径就是protocol,Apple要求开发者提供实现
UITableViewDataSource
UITableViewDelegate 这两个protocol的支持类来协同对应的UITableView的工作。一般情况下,实现delegate protocol的类是UITableViewController,而data source protocol 可以是controller负责,也可以是其他helper类完成。但更多情况下,我更倾向于单独的modal wrapper类来完成。现在很多的建议data source尽可能不要放在controller中,这样有利于解决mass
controller的问题。

鉴于UITableView是如此的重要,iOS替我们定制化了两种类型的tableView:static和dynamic。前者适用于表格内容相对固定的场景,更多的使用在诸如Setting panel,Detail panel的地方;而后者适用于表格内容不固定,行数动态变化的场景,更多的使用在网络请求返回后,将动态的数据进行内容展示等。虽然Apple内置了几种(确切的说到目前为止是4种)table的style,但是对于App开发而言,大多数情况下都需要自行定制table对数据的展示方式,因此相对应的,table
cell的定制化成为App开发的必修课题。

dynamic动态tableview:

#tableview的初始化#

 

    UITableView *tableView = [[UITableView
alloc]initWithFrame:CGRectMake(0,
0, 100,
100)];

    tableView.delegate =
self;

    tableView.dataSource =
self;

    

    //去除cell的分割线

    tableView.separatorStyle =
UITableViewCellSelectionStyleNone;

    [self.view
addSubview:tableView];

#tableview方法 #

//返回section数量

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

//每个section的header的标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    switch (section) {

        case 0:

            return
@"number1";

            break;

            

        default:

            return
nil;

            break;

    }

}

//返回每个section的cell数量

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 3;

}

//绘制cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cell"
forIndexPath:indexPath ];

    //设置cell属性

    cell.name = @"....";

    return cell;

}

//选中cell后

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"%d",indexPath.row);

}

//header的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return
CGFLOAT_MIN;

}

//footer的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return
CGFLOAT_MIN;

}

//cell的高度

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 80;

}

//cell的高度,自适应

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return
UITableViewAutomaticDimension;

}

//删除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath {

    if (editingStyle ==
UITableViewCellEditingStyleDelete) {

        NSLog(@"delete!");

    }

}

# 自定义cel l#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios UITableView