为同一自定义单元使用不同的重用标识符

时间:2016-08-28 19:43:43

标签: ios objective-c uitableview autolayout

我遵循https://stackoverflow.com/a/18746930中提到的概念,但稍微以不同的方式使用自动布局实现动态单元格。我只有一个原型自定义单元格。但我必须根据数据数组向单元格添加多个UILabel,并且必须保持动态单元格高度。

//我的UITableViewCell

@interface CustomCell ()
@property (nonatomic, assign) BOOL didAddLabel;
@end
@implementation CustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

-(void)addLabel:(NSArray*)someAry{

    if(!didAddLabel){

        for (int i=0; i<someAry.count; i++) {

            // Initialize UILabel Programtically ...
            // adding label
            [self.aViewOfContaintView addSubview:aLabel];
        }


        self.heightLayoutConstraintOfaViewOfContaintView.constant = value;
        [self.aViewOfContaintView layoutIfNeeded];

        self.didAddLabel = YES;
    }
}
@end

//我的UIViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //.. .. ..
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    //.. .. ..
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:CellIdentifier];



    // Configure the cell for this indexPath
    //.. .. ..

    //Add Label
    [cell addLabel:anAry];

    [cell layoutIfNeeded];
    return cell;
}

如果我不使用那个检查didAddLabel,那么滚动表粘贴。如果我使用它,那么我只得到四个不同高度的细胞。

上述答案提到“对于小区中的每个唯一约束集,请使用唯一的小区重用标识符。 ......”

如何在同一个自定义单元格中使用/注册不同的重用标识符? 任何替代解决方案或任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

您应该使用手动注册:

This method

您可以使用这两个类/ nib,并为多个重用标识符注册相同的类/ nib。你的出队仍然是相同的

相关问题