UITableviewCell中的两个按钮

时间:2013-09-12 12:24:00

标签: ios

我在UITableViewCell中遇到按钮问题。请帮帮我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        switch (indexPath.section) {
            case 0:
                break;
            case 2:{
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    // Make backgroung invisible
                    [self cellBackground:cell];
                    // Make the save button
                    [self saveButton:cell];

                    [self showButton:cell];

                }
                break;

        }
    } 
    return cell;
}
- (void)saveButton:(UITableViewCell *)cell {
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [saveButton setTitle:@"Save" forState:UIControlStateNormal];
    saveButton.frame = CGRectMake(45, 0, 300, 45);
    saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    saveButton.contentEdgeInsets = UIEdgeInsetsMake(0,10,0,0);

    [cell addSubview:saveButton];
}

- (void)showButton:(UITableViewCell *)cell{
    UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [showButton setTitle:@"Show" forState:UIControlStateNormal];
    showButton.frame = CGRectMake(45, 0, 300, 45);
    showButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    showButton.contentEdgeInsets = UIEdgeInsetsMake(0,10,0,0);

    [cell addSubview:showButton];
 }

我只想在indexPath.section = 2中并排使用saveButton和showButton,并且按钮的大小应该适合横向或纵向的单元格大小。你知道怎么做吗?感谢。

2 个答案:

答案 0 :(得分:1)

你这样做完全错了。我不会浏览一般部分,但你绝对应该继承UITableViewCell并添加saveButtonshowButton作为子类的方法,并在启动类时运行它们。

并将其添加到cell.contentView。不要cell

答案 1 :(得分:-1)

首先,你正在构建错误的单元格:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

这可以返回零,所以你应该检查:

 if(!cell) { 
      //construct new cell here.
 }

为了处理两个旋转,最好将它放置在button.frame.origin.x = cell.view.frame.width - 10.0f并将锚点设置到按钮的右侧。