如何重新创建与默认 tableView:viewForHeaderInSection: 相同的默认 UIView?

2024-04-02

我尝试实施

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

获取标题部分的文本标签为黑色而不是白色,但它看起来与SDK创建的默认文本标签非常不同,我的太丑了。

如何重新创建与SDK中的UIView一模一样的UIView?

来自苹果文档:

讨论 http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006941-CH3-SW13
表视图对节标题标题使用固定字体样式。如果您想要不同的字体样式,请在委托方法 tableView:viewForHeaderInSection: 中返回自定义视图(例如 UILabel 对象)。


尽管这并不完全正确,但我发布此内容是希望有人可以改进我的方法。文本是正确的(默认为白色,但您可以更改它)。背景渐变和不透明度并不完美(太亮??)——这里可能需要进行一些调整。

在这里获取节标题background.png:http://img5.imageshack.us/img5/6616/sectionheaderbackground.png http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

// Create a stretchable image that emulates the default gradient
UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];

// Create the view for the header
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
sectionView.alpha = 0.9;
sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];

// Create the label
CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
sectionLabel.text = @"Test section label";
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.shadowColor = [UIColor grayColor];
sectionLabel.shadowOffset = CGSizeMake(0, 1);
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionView addSubview:sectionLabel];
[sectionLabel release];

// Return the header section view
return sectionView;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何重新创建与默认 tableView:viewForHeaderInSection: 相同的默认 UIView? 的相关文章

随机推荐