【问题标题】:Setting custom header view in UITableView在 UITableView 中设置自定义标题视图
【发布时间】:2013-04-30 18:06:24
【问题描述】:

我正在尝试将自定义视图添加到我的每个 UITableView 部分的标题中。我正在使用此委托方法来返回所需的视图。它的部分工作是因为它导致单元格部分展开,就好像那里有一个标题一样,但是文本或 UIButton 都没有实际出现。我知道这个方法正在被调用,因为我在方法中放置了一个 NSLog 以查看它是否是。我是否犯了某种愚蠢的错误,或者这不是正确的做法?

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView* customView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)];
        customView.backgroundColor = [UIColor clearColor];

        UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44.0)];
        headerLabel.textColor = [UIColor darkGrayColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:16];


        UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        // add button to right corner of section

        return customView;
        switch (section) {
            case 0:
                headerLabel.text = @"Team Name";
                break;
            case 1:
                headerLabel.text = @"Captain";
                break;
            case 2:
                headerLabel.text = @"Wicket Keeper";
                break;
            case 3:
                headerLabel.text = @"Batting Order";
                headerButton.center = CGPointMake( 160.0, 22.0);
                headerButton.backgroundColor = [UIColor blueColor];
                headerButton.tag = section;
                [headerButton   addTarget:self action:@selector(enableCellReordering:) forControlEvents:UIControlEventTouchUpInside];
                [customView addSubview:headerButton];
                break;
            default:
                break;
        }

        [customView addSubview:headerLabel];
        return customView;
    }

【问题讨论】:

  • 你实现了tableView:heightForHeaderInSection:委托方法吗?如果您实现tableView:viewForHeaderInSection:,这是必需的。
  • 为什么要返回customView两次?
  • @SpaceDust 回答了我的问题,这是一个愚蠢的错误!
  • @SpaceDust 不错。在switch 之前对return 的无关调用是问题所在。
  • @simonthumper 因为我是第一个发现你的问题的人,我应该得到批准的答案。

标签: ios uitableview custom-headers


【解决方案1】:

找到了,return customView;switch 声明之前。

【讨论】:

  • 谢谢,你的眼睛比我还敏锐哈哈!
【解决方案2】:

return你的customview两次,一次在switch语句之前,一次在switch语句之后,两次只需要在switch (section)之前删除return customView;

【讨论】:

    【解决方案3】:
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
        /* Create custom view to display section header... */
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
        [label setFont:[UIFont boldSystemFontOfSize:12]];
         NSString *string =[list objectAtIndex:section];
        /* Section header is in 0th index... */
        [label setText:string];
        [view addSubview:label];
        [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
        return view;
    }
    

    【讨论】:

    • 欢迎来到 SO,@user3836191。习惯上提供有关代码功能的说明,以便其他人更容易理解。
    【解决方案4】:

    自定义标题 origin.x 和 origin.y 怎么样? 当我将它们设置为非零时,但它仍然靠近 ledt 边缘。

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      相关资源
      最近更新 更多