UITableView基本操作[三] UITableViewDelegate 和 UITabl...

1、不要忘记设置代码哇

// 创建tableview
    _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
    // 设置数据源 注意这个方法设置不对,没有数据哦!
    _tableView.dataSource = self;
    // 设置表格的代理
    _tableView.delegate = self;

2、UITableViewStylePlain 样式

// 选中一行的时候触发
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView  cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

// 离开一行的时候触发,必须设置代理哦
-  (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%d", indexPath.row);
    DetailViewController *detailVc = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVc animated:YES];
    [detailVc release];
    
}

// 表视图sescations有多少个
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

// 设置行数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

    UITableViewCell  *cell  = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
    NSString *fontName = _listArray[indexPath.row];
    cell.textLabel.text = fontName;
    cell.textLabel.textColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont fontWithName:fontName size:12];
    
    return cell;
}

3、UITableViewStyleGrouped

// sections 数量 sections == 部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_listArray count];
}

// section title 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
    NSString *title = [NSString stringWithFormat:@"这是第%d个Sections", section + 1];
    return title;
}

// sections 有几个 rows 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_listArray[section] count];
}

// 选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"section is :%d", indexPath.section);
    NSLog(@"row is:%d", indexPath.row);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell  *cell  = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
    NSString *fontName = [[_listArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = fontName;
    
    return cell;
}

转载于:https://my.oschina.net/wangdk/blog/150390

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值