UI_UITableView

来源:互联网 发布:淘宝lolita鞋子 编辑:程序博客网 时间:2024/06/05 21:54

The UI of iPhone Settings (09 Day)

RootView.h

@property (nonatomic, retain) UITableView *tableView;

RootView.m

#pragma mark - 添加全部控件- (void)addAllViews{    // 3. 创建表示图并添加    self.tableView = [[[UITableView alloc] initWithFrame:self.frame style:UITableViewStyleGrouped] autorelease];    [self addSubview:_tableView];}

RootViewController.m

- (void)viewDidLoad {    [super viewDidLoad];        // 4. 设置数据源    _rootView.tableView.dataSource = self;}// 5. 遵守数据源协议@interface RootViewController () <UITableViewDataSource>@property (nonatomic, retain) RootView *rootView;@end
// 声明一个大数组属性,用于存放设置里面的title,如果要删除组或者单个元素,可在数组里面直接操作。“根据数据来显示页面”的方式@property (nonatomic, retain) NSArray *allDataArray;self.allDataArray = @[                          @[@"通用", @"隐私"],                          @[@"iCloud", @"地图", @"Safari", @"照片与相机", @"Game Center"],                          @[@"Twitter",  @"Facebook",  @"Flickr", @"Vimeo", @"新浪微博", @"腾讯微博"],                          @[@"开发者"]                          ];// 6. 实现协议里的方法#pragma mark - UITableViewDateSource Methods#pragma mark 设置有多少分区(可选的,如果不实现,默认有一个分组)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    // 分区数就是大数组的元素个数    return _allDataArray.count;}#pragma mark 设置每一个分区有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 通过section,获取小数组,再发送count消息,获取小数组元素个数,并返回    return [_allDataArray[section] count];}#pragma mark 设置每行显示的内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil] autorelease];//    cell.textLabel.text = @"标题";//    cell.detailTextLabel.text = @"详情描述";//    cell.imageView.image = [UIImage imageNamed:@"head_2"];//    NSArray *smallArray = _allDataArray[indexPath.section];//    NSString *str = smallArray[indexPath.row];//    cell.textLabel.text = str;    // 通过indexPath.section从大数组中获取小数组,再通过indexPath.row从小数组中获取字符串并显示    cell.textLabel.text = _allDataArray[indexPath.section][indexPath.row];    return cell;}// 声明一个头标题数组属性,存储头标题@property (nonatomic, retain) NSArray *headTitleArray;    self.headTitleArray = @[@"First", @"Second", @"Third", @"Fourth"];#pragma mark - 设置头标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return _headTitleArray[section];}#pragma mark 设置快速索引- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return @[@"1", @"2", @"3", @"4"];}

使用“重用标识”优化内存,替代上面的方法

#pragma mark 设置每行显示的内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1. 根据重用标识,去重用队列里去查找, 返回可重用的    static NSString *cellIdentifier = @"cellIdentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    // 2. 判断是否有重用的,如果没有,则自己创建    if (cell == nil) {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];    }    // 3. 正常使用cell    cell.textLabel.text = _allDataArray[indexPath.section][indexPath.row];    return cell;   }

遵守协议UITableViewDelegate

#pragma mark 实现协议方法(涉及到动画)- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row % 2 == 0) {        cell.frame = CGRectMake(-375, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);    } else {        cell.frame = CGRectMake(375, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);    }    [UIView animateWithDuration:1.f animations:^{        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);    }];}

一些其他的属性和方法

#pragma mark 设置行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row % 2 == 0) {        return 20;    } else {        return 80;    }}#pragma mark 自定义Header- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 100)] autorelease];    headerLabel.backgroundColor = [UIColor colorWithWhite:.5 alpha:.5];    headerLabel.text = @"头标题";    return headerLabel;}#pragma mark 如果自定义了Header,需要通过下面的方法来设置header的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 100.0f;}#pragma mark 点击方法// 点击后打印它的位置与名称,以及变成黄色- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%ld   %ld", indexPath.section, indexPath.row);    NSLog(@"%@", _allDataArray[indexPath.section][indexPath.row]);    // cellForRowAtIndexPath:通过indexPath,获取到cell    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    cell.backgroundColor = [UIColor yellowColor];    // 设置无选中效果    cell.selected = NO;}
0 0
原创粉丝点击