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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1349571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:07:32  来源:igfitidea点击:

How to customize tableView Section View - iPhone

iphonexcodeuitableview

提问by Sagar R. Kothari

I know how to customize the tableViewCell.

我知道如何自定义 tableViewCell。

I have seen many application customizing the tableView Cell.

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

Similarly, I want to customize TableView Section Header

同样,我想自定义TableView Section Header

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

“假设 - 部分名称应该使用不同的字体,它具有不同的背景图像等。”

Is it possible How?

有可能吗?

In which method Should I implement the code?

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

回答by h4xxr

Instead of using the normal method

而不是使用正常的方法

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

You want to implement this one:

你想实现这个:

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

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.

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

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;

Hope that helps

希望有帮助