ios之UITableView

本文详细介绍了UITableView的基本使用方法,包括如何创建、配置UITableView及其数据源,同时提供了完整的代码示例。文章还介绍了UITableView的重要组成部分NSIndexPath的作用及用法。

        UITableView应用非常广泛,几乎每个应用都会用到它,看起来很复杂,好在上手不难。

        UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格。查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和delegate。

  dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。

  delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。

  提到UITableView,就必须的说一说NSIndexPath。UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代表在该section中的第几行。

       我是在UIViewController中创建的UITableView,在.h文件中遵循下协议,.m文件中遵循两个协议,创建一个成员变量数组,存放tableView数据,代码如下:

.h文件代码:

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_tableArr;
}
@end

.m文件代码:

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableView.separatorColor = [UIColor brownColor];  //设置分割线颜色
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;  //设置分割线样式,是否隐藏
//    tableView.rowHeight = 70;  //设置行高,协议优先级比属性高
    tableView.delegate = self;
    tableView.dataSource = self;

    tableView.sectionHeaderHeight = 50;   //设置section head高度
    [self.view addSubview:tableView];
    [tableView release];
实现两个必须实现的协议方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_tableArr count];
}   //设置tableView行数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify = @"firstCell";   //static:只初始化一次
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];   //从重用池中取数据,这里用的是一个自定义cell样式
    if (!cell) {
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];  //创建cell样式
    }
    NSArray *arr = [_tableArr objectAtIndex:indexPath.section];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    NSString *str = [dic objectForKey:@"name"];
    NSString *detailStr = [dic objectForKey:@"sex"];
    cell.leftImage.image = [UIImage imageNamed:@"1.jpg"];
    cell.Name.text = str

    return cell;
}   //设置cell样式

现在tableView已经建完了,运行程序,数组中的元素就会显示在界面上。下面列举几个常用的TableView协议方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消选中状态
    
//    [self.navigationController pushViewController:<#(UIViewController *)#> animated:<#(BOOL)#>]
}   //点击cell事件

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}     //设置section个数

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSArray *arr = [NSArray arrayWithObjects:@"第一部分",@"第二部分",@"第三部分", nil];
    return [arr objectAtIndex:section];
}  //设置section head的标题

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSInteger row = indexPath.row;
//    NSInteger section = indexPath.section;
    
//    if (section == 0) {
////        if (row == 0) {
////            return 200;
////        }
//        return 50;
//    }
//    if (section == 1) {
//        return 70;
//    }
//    else{
//    return 50;
//    }
    return 100;
}   //设置行高,可以根据需要,设置不同的行高

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        NSArray *arr = [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil];
    return arr;
}  //tableView右侧导航
先写到这,日后再补充。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值