【问题标题】:Using different reuse identifiers for same custom cell对同一个自定义单元格使用不同的重用标识符
【发布时间】:2017-01-04 19:52:05
【问题描述】:

我遵循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,然后滚动表格会粘住。如果我使用它,那么我只会得到四个不同高度的单元格。

上面的答案提到“对于单元格中的每个唯一约束集,使用唯一的单元格重用标识符。 ……”

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

【问题讨论】:

  • 动态添加标签几乎总是一个坏主意。你为什么不使用表格部分呢?或者,一个集合视图?
  • @Sulthan ,感谢您提供的替代方案。但是我想解决这个场景,那我要怎么解决呢?
  • @MehediHasan 你有没有找到解决办法?

标签: ios objective-c uitableview autolayout


【解决方案1】:

您应该使用手动注册:

This method

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

【讨论】:

  • 我在 MyViewController 中有一个带有 CustomCell 的表格视图,标识符是“CellID”。单元格连接到我的 CustomCell 类。如果我使用这样的 NSString *cellID = [NSString stringWithFormat:@"CellID%d", (int)someAry.count]; [self.tableView registerClass:[BookingCell 类] forCellReuseIdentifier:cellID]; CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellID];相反,然后我得到空单元格,可能是因为队列中没有任何单元格。你能提供任何示例代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 2013-07-25
  • 1970-01-01
相关资源
最近更新 更多