如何自定义tableView剖面视图 - iPhone

Sag*_*ari 29 iphone xcode uitableview

我知道如何自定义tableViewCell.

我见过很多应用程序自定义tableView Cell.

同样,我想自定义TableView Section Header

"假设 - 部分名称应该是不同的字体,它有不同的背景图像等."

有可能怎么样?

我应该在哪种方法中实现代码?

h4x*_*xxr 60

而不是使用正常的方法

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

你想实现这个:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

如您所见,第二个返回UIView而不是文本字符串.因此,您可以自定义自己的视图(带标签等)并将其返回.

以下是如何执行此操作的示例(将在上述方法中实现):

// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];

// create image object
UIImage *myImage = [UIImage imageNamed:@"someimage.png"];;

// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.frame = CGRectMake(70,18,200,20);
headerLabel.text =  @"Some Text";
headerLabel.textColor = [UIColor redColor];

UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor darkGrayColor];
detailLabel.text = @"Some detail text";
detailLabel.font = [UIFont systemFontOfSize:12];
detailLabel.frame = CGRectMake(70,33,230,25);

// create the imageView with the image in it
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,50,50);

[customView addSubview:imageView];
[customView addSubview:headerLabel];
[customView addSubview:detailLabel];

return customView;
Run Code Online (Sandbox Code Playgroud)

希望有所帮助

  • 如果我想只有section = 1的头而不是section = 0怎么办?我遇到了这个问题...... (2认同)