iOS开发之UITableView详解

来源:互联网 发布:福建网络继续教育学院 编辑:程序博客网 时间:2024/05/17 04:13

    表示图可谓是iOS开发中最常用的一个控件,没有之一。深入学习之后你会发现他有很多强大的功能,几乎可以用tableView实现所有的UI界面。

//表示图的创建     self.tableView=[[UITableViewalloc]initWithFrame:self.view.frame    style:UITableViewStylePlain];    //设置代理    self.tableView.delegate =self;    self.tableView.dataSource =self;    //设置背景颜色    self.tableView.backgroundColor = [UIColorredColor];    //分割线样式    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;    //分割线颜色    self.tableView.separatorColor = [UIColorblueColor];    //行高    self.tableView.rowHeight =80;

//表示图的一些常用代理方法和数据源方法#pragma mark ---------- UITableViewDelegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"点击方法!!!");}

//返回每一行cell的高度,要比设置rowHeight更灵活- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.section ==0) {        return 66;    } else if (indexPath.section ==1) {        return 88;    }    return 44;}

//头部分区高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 50;}

//尾部分区高度- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 30;}

//自定义分区头部- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *headView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,44)];    headView.backgroundColor = [UIColorgreenColor];    return headView;}

//自定义分区尾部- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIView *footView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,44)];    footView.backgroundColor = [UIColoryellowColor];    return footView;}

#pragma mark -- UITableViewDataSource

//返回分区有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 10;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellIdentifier =@"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if (cell == nil) {       cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];    }    //INSERT CODE    cell.textLabel.text =@"iOS开发";    return cell;}

#pragma mark -- 选择实现的代理方法

//显示有几个分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 3;}

//分区头部标题- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section == 0) {        return @"第一段标题";    } else if (section ==1) {        return @"第二段标题";    }    return@"段头标题";}
//分区尾部标题- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    if (section == 0) {        return @"第一段段尾";    } else if (section ==1) {        return@"第二段段尾";    }    return@"段尾标题";}
//右侧索引内容- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    NSArray *array = @[@"k",@"z",@"s"];    return array;}

1 0