UITableView的基本使用

UITableView作为iOS开发里基本控件,是我们第七个需要学习的。下面我来为大家介绍一下UITableView的一些常用属性和它们的用法。

这里附上UI控件演示的源码地址:https://github.com/LOLR2017/UIKitDemo。源码持续更新中...

因为部分代码是从Xcode直接拷贝出来的样式有误请谅解。

UITableView的内容较多,这里我就只演示基本代码,关于tableView的自定义cell和多样式的不同之处,以及插入和删除,请下载源码。

//初始化一个plan样式的tableView

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

    

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"ID"];

    [self.view addSubview:self.tableView];

    

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];

    headerView.backgroundColor = [UIColor redColor];

    //添加整个tableView的header

    self.tableView.tableHeaderView = headerView;

    

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];

    footerView.backgroundColor = [UIColor greenColor];

    //添加整个tableView的footer

    self.tableView.tableFooterView = footerView;

    

    

    //设置索引字体颜色

//    self.tableView.sectionIndexColor = [UIColor blackColor];

    //设置索引背景颜色

//    self.tableView.sectionIndexBackgroundColor =[UIColor blackColor];

下面是我们UITableView的一些代理方法:

显示每组标题索引

//

//-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

//

//}

//

返回每个索引的内容

//

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

//

//}

//

响应点击索引时的委托方法

//

//-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {

//

//}



//返回tableView有多少组,不写默认返回1组

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

    return 3;

}


//返回tableView每组有多少行

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

    return 5;

}


//返回一个cell,用来显示具体内容

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];

    }

    cell.textLabel.text = [[NSString alloc] initWithFormat:@"第%ld组,第%ld行",(long)indexPath.section,(long)indexPath.row];

    return cell;

}


//返回没行有多高

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

    return 44.0;

}


//返回每组的header高度

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

    return 35;

}


//返回每组的footer的高度

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

    return 35;

}


//设置section的header的具体内容

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ID"];

    UILabel *label = [header viewWithTag:101];

    if (label == nil) {

        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];

        label.textColor = [UIColor blackColor];

        label.font = [UIFont systemFontOfSize:16];

        label.tag = 101;

        [header addSubview:label];

    }

    label.text = [[NSString alloc] initWithFormat:@"第%ld组的header",(long)section];

    return header;

}

//设置section的footer的具体内容

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ID"];

    UILabel *label = [header viewWithTag:101];

    if (label == nil) {

        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];

        label.textColor = [UIColor blackColor];

        label.font = [UIFont systemFontOfSize:16];

        label.tag = 101;

        [header addSubview:label];

    }

    label.text = [[NSString alloc] initWithFormat:@"第%ld组的footer",(long)section];

    return header;

}

在这里我们只是简单的介绍了plan样式的使用,关于自定义样式,插入,删除等操作请下载源码查看,如果还需要tableView更多的功能请留言,我将完善这些功能。

这里附上UI控件演示的源码地址:https://github.com/LOLR2017/UIKitDemo。源码持续更新中...

写在结尾:本文初衷是提供给一些新手或者需要某些资料查询者,代码或者思想有不足之处请大家谅解,希望大家多多原谅。最后希望大家可以共同进步,快乐工作。

上一篇文章: 《UIScrollView的基本使用》
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值