【问题标题】:add multiple horizontal buttons to table view向表格视图添加多个水平按钮
【发布时间】:2012-08-06 03:23:29
【问题描述】:

我已经尝试了很多东西——从对象浏览器中添加一个按钮、更改属性、搜索网络,但没有运气。本质上,我想做——在故事板中——:

您在哪里看到“添加到联系人”、“共享位置”和“添加到书签”。

【问题讨论】:

  • 我意识到我希望能够在情节提要中做到这一点,因为它是一个图形化的东西。我找到了this,但背景是白色的 - 我希望表格背景(灰色垂直条纹)可以通过。

标签: ios uitableview


【解决方案1】:

您应该创建一个UITableViewCell,其contentView 拥有3 个独立的UIButtons。 要以编程方式执行此操作,在您的 tableView:cellForRowAtIndexPath: 数据源方法中,您可以使用类似于以下的代码:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.backgroundColor = [UIColor clearColor];
        UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        UIButton* button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        UIButton* button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button1.frame = UIEdgeInsetsInsetRect(cell.contentView.bounds, UIEdgeInsetsMake(0, 0, 0, 250)); 
        button2.frame = UIEdgeInsetsInsetRect(cell.contentView.bounds, UIEdgeInsetsMake(0, 125, 0, 125)); 
        button3.frame = UIEdgeInsetsInsetRect(cell.contentView.bounds, UIEdgeInsetsMake(0, 250, 0, 0)); 
        button1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        button2.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
        button3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [cell.contentView addSubview:button1];
        [cell.contentView addSubview:button2];
        [cell.contentView addSubview:button3];
    }

    return cell;
}

另外,在您的委托的tableView:willDisplayCell: 方法中,执行以下操作以使单元格的默认装饰完全消失:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundView = nil;
}

您应该获得与您发布的内容非常相似的结果。

【讨论】:

    【解决方案2】:

    将三个按钮放在一个 320 像素宽、60 高的 UIView 中,然后将该视图作为表格的页脚。

    【讨论】:

      【解决方案3】:

      UITableView 使用 UITableViewStyleGrouped 样式设置样式。
      三个UIButtons 以编程方式添加到tableView.tableFooterView
      或者,您可以将三个UIButtons 添加到最后一个cellcontentView

      添加按钮,如:

      UIButton *theButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      theButton.frame = CGRectMake(20, 20, 200, 40); 
      [theButton setTitle:@"Button" forState:UIControlStateNormal];
      [theButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
      [cell.contentView addSubview:theButton];
      

      通过反复试验正确获取按钮位置 :)

      【讨论】:

        猜你喜欢
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多