UITableView SectionHeader 自定义section的头部

//自定义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;
}ios

 

 

 

设置UITableView Section、cell背景颜色

分类: iosUI

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

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

若是想改变此处的背景与字体的话,官方没有开放接口去直接修改以上两个属性,因此,只有本身加Label,加View去实现,代码以下:post

实现委托方法- (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];
ui

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

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

 

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

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

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

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

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

最后经过,在blog

- (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).

相关文章
相关标签/搜索