我试着实现了
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
获取header部分的文本标签为黑色而不是白色,但它看起来与SDK创建的默认文本标签相差甚远,我的太丑了。
如何重新创建与 SDK 中完全相同的 UIView?
来自 Apple 文档:
Discussion 强>
表格 View 为节标题标题使用固定的字体样式。如果您想要不同的字体样式,请改为在委托(delegate)方法 tableView:viewForHeaderInSection: 中返回一个自定义 View (例如,一个 UILabel 对象)。
最佳答案
虽然这并不完全正确,但我发布此内容是希望有人可以改进我的方法。文本是正确的(默认为白色,但您可以更改它)。背景渐变和不透明度并不完美(太亮了??)——可能需要在此处进行一些调整。
在此处获取 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;
关于iphone - 如何重新创建与默认 tableView :viewForHeaderInSection:? 相同的默认 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898361/