码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView SectionHeader 自定义section的头部

时间:2015-01-25 12:27:02      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

//自定义section的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 30)];//创建一个视图
    UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 30)];
    UIImage *image = [UIImage imageNamed:@"4-2.png"];
    [headerImageView setImage:image];
    [headerView addSubview:headerImageView];
    [headerImageView release];
    
    NSString  *createTime = [self.keysArray objectAtIndex:section];
    createTime = [createTime stringByReplacingCharactersInRange:NSMakeRange(4, 1) withString:@"-"];
    createTime = [createTime stringByReplacingCharactersInRange:NSMakeRange(7, 1) withString:@"-"];
    
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 5, 150, 20)];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:15.0];
    headerLabel.textColor = [UIColor blueColor];
    headerLabel.text = createTime;
    [headerView addSubview:headerLabel];
    [headerLabel release];
    
    return headerView;
}

 

 

 

设置UITableView Section、cell背景颜色

分类: iosUI

section所显示的灰色背景和白色字体是默认的,调用以下方法即可实现

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.keys objectAtIndex:section];
}

如果想改变此处的背景与字体的话,官方没有开放接口去直接修改以上两个属性,所以,只有自己加Label,加View去实现,代码如下:

实现委托方法- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* myView = [[[UIView alloc] init] autorelease];
myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
titleLabel.textColor=[UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text=[self.keys objectAtIndex:section];
[myView addSubview:titleLabel];
[titleLabel release];
return myView;
}

Cocoa提供的按钮背景色为透明。因为ContentView被移开,下面是tableView的颜色,已经不是cell的一部分了。
所以,最好的方式应该是通过cell.backgroundView来改变cell的背景。按照文档说明,backgroundView始终处于 cell的最下层,所以,将cell里的其它subview背景设为[UIColor clearColor],以cell.backgroundView作为统一的背景,应该是最好的方式。UIView *backgrdView =[[UIView alloc] initWithFrame:cell.frame];
backgrdView.backgroundColor=[UIColor blueColor];
cell.backgroundView= backgrdView;
[backgrdView release];

在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法中,[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]

ios 动态调整列表行高的经验教训

 

最初的列表界面(UITableView)行高是固定的。所以实现 UITableViewDelegate中的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

返回一个固定的CGFloat类型的数既可。

不久以前,需要将UITableView里面的每个cell高度动态调整。(cell 里面有一个

UILable),UILable.text 长度是可变的。

最后通过,在

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

中调用

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

得的UITableViewCell, 最后通过返回UITableViewCell.frame.size.height 搞定

原因大概是这样:

reload 调用 cellForRowAtIndexPath,但在此之前会先执行heightForRowAtIndexPath

所有在 heightForRowAtIndexPath 里面调用 cellForRowAtIndexPath 不会有问题。

此处有一个明显的问题就是 cellForRowAtIndexPath 会被调用两次。

 

今天又有一个需求:需要在因UITableView 的datasource变化后,导致某一个确定的cell需要被
reload (reloadRowsAtIndexPaths). reload 后需要在该cell中添加一些竖型排列子视图 (addsubview)并且让该subview在可视区域里面。

在reload部分。cellForRowAtIndexPath部分。(reload的时候会自动调用cellForRowAtIndexPath)增 加了部分UIScrollView的scroll相关的代码。(UITableView继承自UIScrollView)。发现 cellForRowAtIndexPath被循环调用。

实在是太给力了。

ios的源代码看不见,但是感觉应该是下面这样的调用序列:

cellForRowAtIndexPath 会调用 UIScrollView的scroll相关的代码。而 UIScrollView的scroll相关的代码 又调用 heightForRowAtIndexPath。heightForRowAtIndexPath则又会调用 cellForRowAtIndexPath.

一个完整的死循环出现了。

所有,在求tableview的行高的时候,万不能图方便在 heightForRowAtIndexPath 调用

cellForRowAtIndexPath 来得到cell相关的高度。不然后果太严重了。

最后的做法,依然是通过计算cell的实际高度在解决。可能会比较复杂。也比较麻烦。但是却是一条比较稳妥的做法。对后面扩展新功能不会有影响。

需要注意的一点是:这个方法里返回视图的大小是固定不变的

The table view automatically adjusts the height of the section header to accommodate the returned view object. The table view does not call this method if it was created in a plain style (UITableViewStylePlain).


UITableView SectionHeader 自定义section的头部

标签:

原文地址:http://www.cnblogs.com/allanliu/p/4247959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!