2011-09-12 29 views
0

我有一個UITableView需要在某些情況下(並非總是)在頂部插入某些東西。Table HeaderView iPhone

代碼(如果它是一個或正常)細胞將如下

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)]; 
imgView.image = [UIImage imageNamed:@"image.png"]; 
cell.imageView.image = imgView.image; 
//sets cell text 
cell.textLabel.text = @"Number"; 

,但我在這裏告訴上,這將是更容易實現headerView比每次改變indexPath。

所以我的問題是我將如何實現這個?我會使用- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section還是使用- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section?請記住,我有一個圖像放入單元格(headerView)。

感謝

回答

2

你會使用

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 

titleForHeaderInSection:只設置標題爲文本,其中作爲viewForHeaderInSection:將允許您添加視圖,UIImageView

0

UIImageView是子類UIView,所以可以從

返回
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

這是你應該做的,因爲它可以完全自定義標題圖像。

如果改用

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 

,那麼你必須控制在唯一的事情是,你返回NSString。您不能添加背景,更改字體顏色等。由於您有圖像,請使用前一種方法。