0

我以这种方式实现了我的自定义单元格:

@implementation StandardCellWithImage: UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Base-List"]];

    _checkbox = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 24, 24)];
    _checkbox.userInteractionEnabled = YES;
    [self addSubview:_checkbox];

    /*textField with image/file name*/
    _nameLabel = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, 180, 44)];
    [_nameLabel setBackgroundColor:[UIColor clearColor]];
    [_nameLabel setFont:[UIFont fontWithName:@"HelveticaNeueCE-Bold" size:18.0f]];
    [_nameLabel setUserInteractionEnabled:NO];
    _nameLabel.keyboardType = UIKeyboardTypeAlphabet;
    _nameLabel.returnKeyType = UIReturnKeyDone;
    [self addSubview:_nameLabel];
}
return self;
}

我在 tableView 中使用它。问题是当我交换单元格以删除时,左侧只出现红色减号,但单元格不会向右移动。如下图所示:

在此处输入图像描述

如何解决此问题以将我的自定义单元格向右移动?

4

2 回答 2

2

你只需要在你的代码中做些小改动

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Base-List"]];

    _checkbox = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 24, 24)];
    _checkbox.userInteractionEnabled = YES;

    [self.contentView addSubview:_checkbox]];//also change here 

  //  [self addSubview:_checkbox];

    /*textField with image/file name*/
    _nameLabel = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, 180, 44)];
    [_nameLabel setBackgroundColor:[UIColor clearColor]];
    [_nameLabel setFont:[UIFont fontWithName:@"HelveticaNeueCE-Bold" size:18.0f]];
    [_nameLabel setUserInteractionEnabled:NO];
    _nameLabel.keyboardType = UIKeyboardTypeAlphabet;
    _nameLabel.returnKeyType = UIReturnKeyDone;
  //  [self addSubview:_nameLabel];


//  i just did change at one line now test your code
    [self.contentView addSubview:_nameLabel];

}
return self;
}

我确实使用 [self.contentView addSubview:_nameLabel];和工作正常,例如:-

我的 UITableIVew:-

在此处输入图像描述

编辑时:-

在此处输入图像描述

于 2013-01-17T09:34:59.437 回答
1

autoresizingMask属性设置为您的UITableViewCell Label. 像

_nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
于 2013-01-17T09:44:53.463 回答