iOS UITableView使用练习 —— HERO博客

上一篇简述了UITableView的属性及方法,本篇练习使用。

具体属性及方法可以参考上一篇UITableView简介:UITableView简介

写了个简单的例子,控制器继承UITableViewController,不需要再去手动添加协议,设置代理。该例子创建了头部视图,tableView分成两组,cell复用,点击替换label,效果如图31-1:



下面贴上代码:(目的学习,实际开发中遵循MVC等设计模式,后续博客会更新相关内容)

#import <UIKit/UIKit.h>

@interface HWTableViewController : UITableViewController

@end


#import "HWTableViewController.h"

@interface HWTableViewController ()

@end

@implementation HWTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    //创建头部视图
    [self creatHeaderView];
}

- (void)creatHeaderView
{
    //创建一个标签作为头部视图
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    label.text = @"这是头部视图";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor grayColor];
    self.tableView.tableHeaderView = label;
}

#pragma mark - Table view data source
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//组中行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 5;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell复用,唯一标识
    static NSString *identifier = @"HWCell";
    //先在缓存池中取
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //缓存池中没有再创建,并添加标识,cell移出屏幕时放入缓存池以复用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    //图片
    cell.imageView.image = [UIImage imageNamed:@"hero"];
    //标题
    cell.textLabel.text = @"这是标题";
    //副标题
    cell.detailTextLabel.text = @"这是副标题";
    //右侧按钮样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取到当前被点击的cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *str = [NSString stringWithFormat:@"点击了第%ld组第%ld行", indexPath.section, indexPath.row];
    
    cell.textLabel.text = str;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0f;
}

//设置头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"第一组";
    }
    return @"第二组";
}

//设置尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"这是尾部标题";
}

@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值