如何自定义tableView剖面视图 - iPhone [英] How to customize tableView Section View - iPhone

查看:86
本文介绍了如何自定义tableView剖面视图 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何自定义tableViewCell。

I know how to customize the tableViewCell.

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

I have seen many application customizing the tableView Cell.

同样,我想自定义TableView Section Header

Similarly, I want to customize TableView Section Header

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

"Suppose - A section name should be in different font, It has different background images etc."

可能如何?

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

In which method Should I implement the code?

推荐答案

而不是使用普通方法

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

你想实现这个:

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

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

As you can see, the second one returns a UIView instead of just string for text. Therefore you can customise your own view (with labels etc) and return it.

以下是您可以如何做到这一点的示例(将在上述方法中实现) :

Here is an example of how you might do that (to be implemented in the above method):

// 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;

希望有所帮助

这篇关于如何自定义tableView剖面视图 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆