0

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

各种autoresizingMask UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;

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

我在自定义单元格中的代码最初是空的,只是 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;
    }
}
4

2 回答 2

0

是xib中的tableview吗?在这种情况下,您必须设置 autosize 属性.. 这样它就不会重叠。设置 autosize top、bottom、right 和 left 属性在 xib 检查器中给出的图像中用于 autosize。

于 2013-08-14T09:17:53.330 回答
0

如果您以编程方式执行 UI,请确保

  1. 您的所有单元格的子视图都添加到 contentView
  2. 所有约束都链接到 contentView(不是单元格本身)
于 2019-02-13T22:03:51.503 回答