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

相關文章
相關標籤/搜索