UITableView的基本使用(一)

来源:互联网 发布:旺旺怎么开通淘宝店铺 编辑:程序博客网 时间:2024/06/05 04:54
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //设置tableView的数据源,通过数据源的协议方法可以设置tableView显示的内容。    _table.dataSource = self;            //设置tableView的代理,通过代理方法可以得到tableView的一些操作响应和设置tableView的一些属性。    _table.delegate = self;            //设置tableView的统一行高。    _table.rowHeight = 100;            //设置tableView的统一区头高度    _table.sectionHeaderHeight = 30;}    //设置每一行的行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return indexPath.row%2?50:100;}    //plain样式的table区头始终在区的最上方,不会随着区向上移动,直到整个区被滑出table之外。    //grouped样式table的区头会随着区一起滑动。    //设置某个区的区头的标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return [NSString stringWithFormat:@"第%lu区的区头",section];}    //设置某个区的区尾的标题- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return [NSString stringWithFormat:@"第%lu区的区尾",section];}    //设置tableView有几个区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 5;}    //设置tableView每个区的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (section == 0) {        return 5;    }else{        return 7;    }    }    //tableView每一行显示的内容非常丰富,所以我们需要把显示的内容排列在一个单元格上,然后把单元格返回。    //设置tableView每一行的单元格    //在某一行的cell将要显示时调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        //*cell的重用机制:每次需要显示一个cell时,先去tableView中寻找可以重用的cell,如果能够找到就直接使用,如果找不到,那么就创建一个新的cell。            //dequeueReusableCellWithIdentifier根据重用标示符在tableView中寻找一个可以重用的cell。如果能够找到那么返回这个cell,如果没有可以重用的cell,那么返回nil    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];        if (!cell) {        NSLog(@"调用了");        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];                    //cell的autorelease必须写在if语句括号中,因为只有创建的cell才release,重用找到的cell不需要release        [cell autorelease];    }        //indexPath.row行号        //indexPath.section区号    cell.textLabel.text = [NSString stringWithFormat:@"第%ld区,第%ld行",indexPath.section,indexPath.row];    cell.imageView.image = [UIImage imageNamed:@"123.png"];    cell.detailTextLabel.text = @"细节";    cell.accessoryType = UITableViewCellAccessoryDetailButton;    return cell;}    //当tableView的某一行被点击时调用。- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

7 0