UI_UITableView

本文详细介绍了如何在 iOS 应用中使用 UITableView 控件来展示数据。从 UITableView 的初始化到代理方法的实现,包括如何设置行高、创建可重用的单元格、设置分区标题等关键技术点。

这里写图片描述

RootViewController.m

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *arr;

@end

@implementation RootViewController

1.数组初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;

    2.当页面只有一个tableview能滚动的视图时,导航栏会自动把坐标系的起点放到(0, 64)的位置,既能保留半透明效果,也方便视图的布局.
    self.automaticallyAdjustsScrollViewInsets = YES;

    3.创建一个UITableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    [tableView release];

    4.设置行高.
    tableView.rowHeight = 100;

    5.签订代理人.dataSource
    tableView.dataSource = self;

    6.设置代理人.
    tableView.delegate = self;
}
7.这个是dataSource实现的第一个协议方法,放回每个分区有多少行,分区默认是1个.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section % 2 == 0) {
        return self.arr.count;
    } else {
        return 10;
    }
    // 数组元素个数跟行数进行关联.  
}
8.只要操作tableview就会触发这个方法,完成对cell的创建和赋值的作用,并且重用的方式完成cell的创建和取用.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    1.指定一个重用标志的字符串.
    static NSString *reuse = @"reuse";
    2.tableview根据指定的重用标志,在重用池里寻找有没有闲置的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    3.如果没有闲置的cell,需要创建一个cell
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
//        NSLog(@"我被创建了");
    }

    4.cell创建好之后对cell上的属性进行赋值.
    cell.textLabel.text = self.arr[indexPath.row];
    cell.detailTextLabel.text = @"2";
    cell.imageView.image = [UIImage imageNamed:@"soldier.jpg"];
//    NSLog(@"%ld", indexPath.row);
    return cell;
}
9.tableView上的索引.
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return @[@"A", @"B", @"C"];
}
10.指定当前的分区个数.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    return 5;
}
11.指定每个分区的标题.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"潘金莲";
    } else if (section == 1) {
        return @"阎婆惜";
    } {
        return @"西门庆";
    }
}
12.如果想写自定义标题,就要和上面的方法一起写,缺一不可.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 24)] autorelease];
    headerView.backgroundColor = [UIColor clearColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(300, 0, 50, 24);
    [button setTitle:@"更多" forState:UIControlStateNormal];
    [headerView addSubview:button];

    return headerView;
}
13.tableview的点击方法.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"当前行数为:%ld", indexPath.row);
    NSLog(@"当前分区数为:%ld", indexPath.section);
    NSLog(@"当前点击人的姓名为:%@", self.arr[indexPath.row]);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"当前的cell为:%@", cell.textLabel.text);
}
14.返回行高.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        return 150;
    } else {
        return 100;
    }
}
15.设置分区标题高度.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section % 2 == 0) {
        return 50;
    } else {
        return 10;
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值