//自定義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背景顏色
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];
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).