ios - 如何获取分组 TableView 的标题 View ?

标签 ios objective-c uitableview uikit

我想获取分组表格 View 的所有 View 以更改标签颜色并设置背景颜色。

最佳答案

我找到了答案,无法获取 TableView 部分的标题 View 。但是您可以实现委托(delegate) tableView:viewForHeaderInSection: 来重新创建标题 View 和标签。以下代码将为您提供相同的标题 View 和确切的标签。

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

    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];

    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 5.5f, 300.0f, 30.0f)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:16.5];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
    view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [view addSubview:label];

    return view;
}

关于ios - 如何获取分组 TableView 的标题 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773703/

相关文章:

objective-c - NSComboBox 数据源和 reloadData

ios - 仅当 indexPath 对象可用时如何在 tableview 单元格上使用标签

ios - 如何使用代码将 UITableView 放在 UILabel 下方

iOS客户端服务器方法

ios - 在 UIPasteBoard 中复制 NSAttributedString

ios - UIView 的 clipsToBounds 和 CALayer 的 masksToBounds 是什么关系?

iphone - NSMutableArray 索引删除

iphone - 为什么 Core Data 比 SQLite 快

ios - 难以更改 UITableViewCell 中图像的大小

iphone - 显示和隐藏键盘时调整表格 View 的大小