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

标签 iphone objective-c uitableview

我试着实现了

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

相关文章:

iphone - 将 youtube channel 加载到 uitableview

ios - 在 UIView 上设置 UIMenuController

iOS-如何修复来自 XCode 中模态转场的保留周期?

ios - 为什么展开 TableViewCell 时我的标签会移动,为什么点击时会出现不同的单元格动画?

ios - UITableView 的 subview 不显示

iphone - MKMapView:获取相对缩放比例?

ios - Xcode 不显示配置文件列表

iphone - iOS:我可以手动将 wifi 网络与地理位置相关联吗?

iphone - 按比例缩小的图像在移动 safari 中模糊不清(仅在一页上)

ios - 如何在 Swift 中动态设置 UITableView 高度?