Fork me on https://github.com

UITableView, 表视图

UITableView, 表视图

 

    样式

    1.UITableViewStylePlain, 正常样式

    2.UITableViewStyleGrouped,  分组样式

    行高, 默认44
    tableView.rowHeight = 80;
    分隔线的颜色
    tableView.separatorColor = [UIColor orangeColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    分隔线间距(上 左 下 右)
    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    表头(重在显示内容)
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
    label.text = @"表        头";
    label.backgroundColor = [UIColor colorWithRed:1.000 green:0.999 blue:0.599 alpha:1.000];
    tableView.tableHeaderView = label;
    label.textAlignment = NSTextAlignmentCenter;
    [label release];
    表尾(重在去除虚拟行数)
//    UIView *footerView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
//    footerView.backgroundColor = [UIColor colorWithRed:0.741 green:1.000 blue:0.836 alpha:1.000];
//     [footerView release];
//    UIView *footerView = [[UIView new] autorelease];
    tableView.tableFooterView = [[UIView new] autorelease];

 附加视图样式

    UITableViewCellAccessoryNone, 无

    UITableViewCellAccessoryDisclosureIndicator, >

UITableViewCellAccessoryDetailDisclosureButton, ! + >
UITableViewCellAccessoryCheckmark, √
UITableViewCellAccessoryDetailButton, !
    数值数据源, 由谁为tableView提供数据
    tableView.dataSource = self;
- (void)createData {
    1.数组 + 字典
//    NSDictionary *person1 = @{@"name": @"陈强", @"Phone": @"15776995908"};
//    NSDictionary *person2 = @{@"name": @"蹦蹦", @"Phone": @"18336394321"};
//    NSDictionary *person3 = @{@"name": @"炳焱", @"Phone": @"15238841866"};
//    
//    self.personArray = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
    2.数组 + 数据模型
//    Person *p1 = [Person personWithName:@"小强" teleNumber:@"15776995908"];
//    Person *p2 = [Person personWithName:@"阿呆" teleNumber:@"14646546486"];
//    Person *p3 = [Person personWithName:@"阿狸" teleNumber:@"74641356413"];
//    
//    self.personArray = [NSMutableArray arrayWithObjects:p1, p2, p3, nil];
    3.分区数据
    //分区1
    Person *p1 = [Person personWithName:@"阿强" teleNumber:@"15776995908"];
    Person *p2 = [Person personWithName:@"阿呆" teleNumber:@"14646546486"];
    Person *p3 = [Person personWithName:@"阿狸" teleNumber:@"74641356413"];
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:p1, p2, p3, nil];
    //分区2
    Person *p4 = [Person personWithName:@"包强" teleNumber:@"15776995908"];
    Person *p5 = [Person personWithName:@"保呆" teleNumber:@"14646546486"];
    Person *p6 = [Person personWithName:@"宝狸" teleNumber:@"74641356413"];
    Person *p7 = [Person personWithName:@"豹虎" teleNumber:@"54666566583"];
    NSArray *array2 = @[p4, p5, p6, p7];
    //分区3
    Person *p8 = [Person personWithName:@"超强" teleNumber:@"15776995908"];
    Person *p9 = [Person personWithName:@"超呆" teleNumber:@"14646546486"];
    Person *p10 = [Person personWithName:@"曹狸" teleNumber:@"74641356413"];
    Person *p11 = [Person personWithName:@"曹虎" teleNumber:@"54666566583"];
    Person *p12 = [Person personWithName:@"超期" teleNumber:@"54666566583"];
    NSArray *array3 = @[p8, p9, p10, p11, p12];

    self.personArray = [NSMutableArray arrayWithObjects:array1, array2, array3, nil];
    
    self.navigationItem.title = @"表视图";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemRefresh) target:self action:@selector(refresh)] autorelease];
    
}

- (void)refresh {
    NSMutableArray *array = self.personArray[0];
    if ([self.personArray[0] count] > 0) {
         [array removeObjectAtIndex:0];
    //tableView的数据源发生变化, tableView不会跟着变化, 需要刷新一下tableView
    UITableView *tableView = (UITableView *)[self.view viewWithTag:1028];
    //重新加载数据
    [tableView  reloadData];
    }
}
Person.m
#import "Person.h"

@implementation Person

- (void)dealloc {
    [_name release];
    [_teleNumber release];
    [super dealloc];
}

- (instancetype)initWithName:(NSString *)name teleNumber:(NSString *)teleNumber {
    self = [super init];
    if (self) {
        self.name = name;
        self.teleNumber = teleNumber;
    }
    return self;
}

+ (instancetype)personWithName:(NSString *)name teleNumber:(NSString *)teleNumber {
    
    return [[Person alloc] initWithName:name teleNumber:teleNumber];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name:%@ teleNumber:%@", _name, _teleNumber];
}
@end

#pragma mark - UITableViewDataSource
分区个数, 默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.personArray.count;
}

分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.personArray objectAtIndex:section] count];
}

cell的创建
当要显示一个cell的时候, 就会执行此方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%s", __FUNCTION__);
    
    NSIndexPath, 继承于NSObject, 用于存储cell的位置(分区和行数)
//    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
    
    UITableViewCell, 单元格, 继承于UIView, 用于展示数据
    样式
    1.Default: 左imageView, 右textLabel
    2.value1: 左imageView, 中textLabel, 右detailTextLabel
    3.value2: 左textLabel, 右detailTextLabel
    4.subtitle: 左imageView, 右上textLabel, 右下detailTextLabel
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"辉哥好帅"];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld, %ld", indexPath.section, indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"li.jpg"];
    cell.detailTextLabel.text = @"我喜欢桃子";
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.665 green:1.000 blue:0.962 alpha:1.000];
    accessoryView, 附加视图
//    cell.accessoryView =
    附加视图样式
    UITableViewCellAccessoryNone, 无
    UITableViewCellAccessoryDisclosureIndicator, >
    UITableViewCellAccessoryDetailDisclosureButton, ! + >
    UITableViewCellAccessoryCheckmark, √
    UITableViewCellAccessoryDetailButton, !
    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    return [cell autorelease];

    重用机制创建cell
    指定重用标识符
    static NSString *string = @"慧慧";
    根据重用标识符, 去重用池中找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
   判断是否找到cell
    if (cell == nil) {
        没有cell, 创建cell
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:string] autorelease];
        NSLog(@"+++");
    }
    cell赋值
//    cell.imageView.image = [UIImage imageNamed:@"li.jpg"];
//    cell.textLabel.text = @"你是我心中的玫瑰花";
    1
    NSDictionary *dic = [self.personArray objectAtIndex:indexPath.row];;
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.text = [dic objectForKey:@"Phone"];
    2
    Person *person = [self.personArray objectAtIndex:indexPath.row];
    cell.textLabel.text = person.name;
    cell.textLabel.text = [[self.personArray objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = person.teleNumber;
    NSArray *array = self.personArray[indexPath.section];
    Person *person = array[indexPath.row];
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.teleNumber;
    
    //返回cell
    return cell;
}

区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *titleArray = @[@"  A", @"  B", @"  C"];
    return titleArray[section];
}

区尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return @"";
}

分区索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return @[@"A", @"B", @"C"];
}

 

   设置代理, 检测tableView
    tableView.delegate = self;

 

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld %ld", indexPath.section, indexPath.row);
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    //赋值
    detailVC.person = self.personArray[indexPath.section][indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
@end
DetailViewController.m
#import "DetailViewController.h"
#import "Person.h"
@interface DetailViewController ()
@end

@implementation DetailViewController

- (void)dealloc {
    [_person release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.632 green:1.000 blue:0.968 alpha:1.000];
    self.name 和 _name 的使用场景
    1.赋值时, 使用self.name = @"zhangsan", self.name会调用setter方法, setter方法内部会处理(retain, copy);
    2.取值时, 使用_name, 如果使用self.name, 会调用getter方法, getter方法返回_name, 和直接使用_name是一样, 并且直接使用_name效果会更高一些
    self.navigationItem.title = @"联系人";
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 275, 40)];
    nameLabel.backgroundColor = [UIColor whiteColor];
    nameLabel.text = [NSString stringWithFormat:@" 姓名: %@", _person.name];
    [self.view addSubview:nameLabel];
    [nameLabel release];
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 170, 275, 40)];
    phoneLabel.backgroundColor = [UIColor whiteColor];
    phoneLabel.text = [NSString stringWithFormat:@" 电话: %@", _person.teleNumber];
    [self.view addSubview:phoneLabel];
    [phoneLabel release];
}

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

UITableView的创建

1.开辟空间, 设置frame, 指定样式

2.设置属性(行高, 分隔线, 表头, 表尾)

3.添加到父视图

4.释放 

UITableView的结构

一个UITableView有一个tableHeaderView, 一个tableFooterView, 多个section

一个section有多个UITableViewCell, 一个区头, 一个区尾

一个UITableViewCellcontentViewaccessoryView, 其中contentViewimageView, textLabel, detailTextLabel

UITableView, 如何展示数据

1.设置dataSource

2.遵守协议<UITableViewDataSource>

3.实现协议方法, 其中有连个方法是required(分区行数, cell的创建)

通过重用机制创建cell

1.重用标识符(static)

2.tableView根据重用标识符去复用池中找cell

3.判断是否找到cell, 如果没有找到cell, 就创建cell

4.cell展示数据

5.返回cell

UITableViewDataSource中的方法

1.分区个数

2.分区的行数

3.创建cell

4.区头标题

5.区尾标题

6.分区索引标题

UITableViewDelegate中的方法

1.选中某一行

如何为UItableView提供数据源

数组 + 字典 + 自定义类

: 当数据源发生变化时, tableView不会跟着改变, 需要重新加载tableView(reloadData)

 

 

posted @ 2015-10-31 19:52  OrangesChen  阅读(301)  评论(0编辑  收藏  举报