UI_UITableView_搭建

本文详细介绍如何使用UITableViewStyle创建UITableView,并实现其基本配置,包括设置数据源、代理方法、单元格内容及高度等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建 tableView

UITableViewStyle 有两种选择

#pragma mark - 创建 tableView
- (void)createTableView
{
    // 枚举类型共同拥有两个
    self.mainTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self addSubview:self.mainTableView];
}

根视图控制器遵守协议

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置数据源代理
    self.rootView.mainTableView.dataSource = self;

    // 设置 delegate
    self.rootView.mainTableView.delegate = self;

    self.title = @"联系人";

}

重写代理方法

返回分组的个数

#pragma -mark 返回分组的个数
// 默觉得 1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

每一个分组显示多少行数据 必须

#pragma mark - 每一个分组显示多少行数据 *必须*
// dataSource 下方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.nameArray.count;
}

每行显示什么内容 必须

#pragma mark - 每行显示什么内容 *必须*
// dataSource 下方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    // 四种类型
    // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];


    // static 作用
    /**
     *  1、清零功能 static int a;
     *  2、保值作用
     *  3、隐藏功能 
     */

    // 静态变量 保值作用 仅仅创建一次
    static NSString *cell_id = @"UITableViewCell";
    // 利用重用创建
    UITableViewCell *cell = nil;

    // 在重用池查找
    cell = [tableView dequeueReusableCellWithIdentifier:cell_id];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }



    cell.textLabel.text = self.nameArray[indexPath.row];

//    indexPath.section  那个分组
//    indexPath.row    哪一行
    NSString *name = [NSString stringWithFormat:@"%ld.png", indexPath.row + 1];
    cell.imageView.image = [UIImage imageNamed:name];

    // 显示具体信息 cell 类型用 UITableViewCellStyleSubtitle
    cell.detailTextLabel.text = self.numberArray[indexPath.row];

    // 右側附件button 枚举类型
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

row 的高度

#pragma mark - 跳转 row 的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}

返回分组的名字

#pragma mark - 返回分组的名字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"分组一";
    }
    return @"分组二";
}

设置分组头部

#pragma mark - 设置分组头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor redColor];
    if (section == 0) {
        label.text = @"1组";
    } else {
        label.text = @"2组";
    }
    return label;
}

设置分组头部的高度

#pragma mark - 设置分组头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60.0f;
}#pragma mark - 设置分组头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60.0f;
}

cell 的点击事件

#pragma mark - cell 的点击事件
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    SecondViewController *secondVC = [[SecondViewController alloc] init];

    [self.navigationController pushViewController:secondVC animated:YES];
}

实现索引

#pragma mark - 实现索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"1组", @"2组"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

转载于:https://www.cnblogs.com/cxchanpin/p/7255702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值