2011-09-12 33 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。您不能添加背景,更改字体颜色等。由于您有图像,请使用前一种方法。