UITableView上的删除按钮覆盖了我的UITableViewCell按钮

时间:2013-08-14 07:50:34

标签: iphone uitableview

我有一个自定义的UITableViewCell,右边有几个标签和3个按钮。我在Storyboard中使用了原型单元格。显示很好但是当我滑动以删除表格中的行时,删除按钮显示在单元格内容的顶部。我已经尝试了两件事。有一件事我想知道,为什么在Storyboard原型单元中设计时没有提供contentView?

各种autoresizingMask UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;

控件添加到contentView中         [self.contentView addSubview:self.buttonView];

我的自定义单元格中的代码最初是空的,只是在h文件中的IBOutlets和自动生成的m文件,我没有添加任何东西,但我现在尝试跟随它并没有任何区别

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialize controls
        self.accountName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.accountId.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.offerName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.offerCode.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.createdDate.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.createdUser.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.orderValue.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.buttonInfo.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.buttonEdit.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
        self.buttonView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;

        // Add controls to Content View
        [self.contentView addSubview:self.accountName];
        [self.contentView addSubview:self.accountId];
        [self.contentView addSubview:self.offerName];
        [self.contentView addSubview:self.offerCode];
        [self.contentView addSubview:self.createdDate];
        [self.contentView addSubview:self.createdUser];
        [self.contentView addSubview:self.orderValue];
        [self.contentView addSubview:self.buttonInfo];
        [self.contentView addSubview:self.buttonEdit];
        [self.contentView addSubview:self.buttonView];
    }
    return self;
}

我在tableView中的代码

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"SavedOrderCell";

    if ([self.orders count] == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return cell;
    }
    else {
        SavedOrderCell *cell = (SavedOrderCell*)[tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.parentVC = self;

        [cell.buttonEdit addTarget: self action: @selector(editProduct:) forControlEvents: UIControlEventTouchUpInside];

        // Configure the cell.
        Order *order = (Order *)[self.orders objectAtIndex:indexPath.row];

        cell.accountName.text = order.orderAccount.name;
        cell.accountId.text = [NSString stringWithFormat:@"ID: %i", [order.orderAccount.accountId intValue]];
        cell.offerName.text = order.orderPromotion.name;
        cell.offerCode.text = order.orderPromotion.code;
        cell.createdDate.text = [NSDateFormatter localizedStringFromDate:order.createdDate dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle];
        cell.createdUser.text = order.createdUser;
        cell.orderValue.text = [Common getCurrencyFormattedStringFromNumber:order.orderTotal];

//        [cell.buttonStatus setImage:[UIImage imageNamed:[NSString stringWithFormat:@"status_%i", [order.statusId intValue]]] forState:UIControlStateNormal];

        UIImageView *bgImage = [[UIImageView alloc] initWithFrame:cell.backgroundView.frame];
        bgImage.backgroundColor = [UIColor clearColor];
        bgImage.opaque = NO;

        if (indexPath.row == 0)
            bgImage.init.image = [UIImage imageNamed:@"table_1col_mid_top.png"];
        else
        {
            if (indexPath.row % 2) {
                bgImage.init.image = [UIImage imageNamed:@"table_1col_light.png"];
            }
            else{
                bgImage.init.image = [UIImage imageNamed:@"table_1col_mid.png"];
            }
        }

        cell.backgroundView = bgImage;

        return cell;
    }
}

2 个答案:

答案 0 :(得分:0)

是xib中的tableview吗?在这种情况下,您必须设置自动调整大小属性..以便它不会重叠.set自动调整顶部,底部,右侧和左侧属性是在xib检查器中为自动调整大小给出的图像中。

答案 1 :(得分:0)

如果您正在以编程方式执行用户界面,请确保

  1. 您单元格的所有子视图都添加到contentView
  2. 所有约束都链接到contentView(而不是单元格本身)