UITableView表视图简单使用


一、表视图的使用场景

表视图 UITableView, 继承自UIScrollView,所以可以滚动, iOS中最重要的视图,随处可见。

表视图通常用来管理一组具有相同数据结构的数据。

表视图的每一条数据都是显示在UITableViewCell对象中

   表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始

二、表视图的创建及显示数据

先介绍下UITableView得相关属性:

重要属性

style样式

plain

group

分割线样式

separatorStyle

分割线颜色

separatorColor

行高

rowHeight


下面是部分创建UITableView的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent = NO;

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    tableView.rowHeight = 40;
    tableView.separatorColor = [UIColor redColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [lb setText:@"You are my music King"];
    [lb setBackgroundColor:[UIColor greenColor]];
    tableView.tableHeaderView = lb;

    //UITableView的两套协议
    //tableView的delegate的方法如非必要,尽量不要实现
    tableView.delegate = self;
    NSLog(@"%@",tableView.delegate);
    NSLog(@"tableView  delegate%s === %d",__func__,__LINE__);
    tableView.dataSource = self;
    NSLog(@"%@",tableView.dataSource);
    NSLog(@"tableView dataSource  === %s  %d",__FUNCTION__,__LINE__);


    [self.view addSubview:tableView];
    [tableView release];
}


三、表视图的重用机制

TableView的数据源UITableViewDataSource

DataSource

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


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



创建多个分区方法:

DataSource

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

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


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 


TableView的委托UITableViewDelegate

Delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

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


如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加。

自定义区头区尾

重要属性

设置图片

imageView

设置文本

textLabel

指定选中效果

selectionStyle

指定辅助效果样式

accessoryType

选中背景图

        selectedBackgroundView


自定义区头区尾方法:

Delegate

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

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

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

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


四、表视图的相关配置方法

NSIndexPath

           是Foundation框架中的一个普通的类,它提供了到嵌套数列的树中特定节点的路径,事实上,它是一个整数阵列,表格视图使用这个去表现在特定章节中的特定行,UITableView用的所有索引路径正好有两个元素,第一个是章节,第二个是行。

重要属性

NSIndexPath


row

section

+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section


//NSIndexPath和TableViews
@interfaceNSIndexPath (UITableView) {
}
+(NSIndexPath*)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
@property(nonatomic,readonly)NSUIntegerrow;
@property(nonatomic,readonly)NSUIntegersection;
@end

SingleSection Table View
//返回行数
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return[myStringcount];
}
//请求时提供一个单元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = ......;
cell.textLabel.text=[myStringobjectAtIndex:indexPath.row];
return[cell autorelease];
}
相关方法实现:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    } else {
        return 2;
    }

    return [_tableArray count];
}
//将要出现cell的时候执行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //声明伪静态字符串来标示重用池,目的是我们不需要管理内存。
    static NSString *cellIdentity = @"_cell";
    //在重用池中把cell取出来
    CustomTableViewCell *cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentity];
    if (!cell) {
        //创建一个cell到重用池里面,方便下次调用
        cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.showsReorderControl = YES;
    }
    //[cell.textLabel setText:@"黑猫"];
    cell.imageView.image = [UIImage imageNamed:@"10.jpeg"];
    cell.detailTextLabel.text = @"First Love";
    NSLog(@"cell ==== %@",cell);
//    NSLog(@"section == %d  row ==  %d",indexPath.section,indexPath.row);
    NSString *str = [_tableArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:str];
    return cell;
}
#pragma mark 指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
    return 4;
}

#pragma mark 设置每个分组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"那些年我们一起听过的情歌";
}
#pragma mark 设置每个行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 40;
        } else if (indexPath.row == 1)
        {
            return 80;
        }
        else if (indexPath.row == 2){
            return 100;
        }
    }
    return 70;
}
#pragma mark 标示图Section头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

#pragma mark 标示图Section尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}
//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *titleArray = [NSMutableArray array];
    for (int i = 65; i <= 90; i++) {
        [titleArray addObject:[NSString stringWithFormat:@"%c", i]];
    }
    return titleArray;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值