xcode 如何自定义tableView截面视图- iPhone

lymnna71  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(124)

我知道如何自定义tableViewCell。
我见过许多应用程序定制tableView单元格。
同样,我想自定义TableView节标题
“假设-一个部分的名称应该在不同的字体,它有不同的背景图像等.”
有可能吗怎么可能?
我应该用哪种方法实现代码?

polhcujo

polhcujo1#

而不是使用正常的方法

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

您希望实现以下功能:

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

如你所见,第二个返回一个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;

相关问题