【问题标题】:Dealing with UITableViewCells partially defined in xib partially augmented by code处理部分在 xib 中定义的 UITableViewCells 部分由代码增强
【发布时间】:2018-04-15 03:49:26
【问题描述】:

我有一些单元格在 Xib 中定义了一些静态内容和一些动态内容(单元格内的表格),这取决于数据模型。

所以我晚点注册 Nibs 以获得合成标识符:

 #define LEFTCELLXIB @"P97PromoCardsLeft"
 #define RIGHTCELLXIB @"P97PromoCardsRight"
 #define TOPCELLXIB @"P97PromoCardsTop"
 #define BOTTOMCELLXIB @"P97PromoCardsBottom"

 #define LEFTREUSEPREFIX @"PromoCardImageLeft"
 #define RIGHTREUSEPREFIX @"PromoCardImageRight"
 #define TOPREUSEPREFIX @"PromoCardImageTop"
 #define BOTTOMREUSEPREFIX @"PromoCardImageBottom"

- (NSString *)getCellIdentifier
{
    // cell height varies
    NSString *ident = [NSString stringWithFormat:@"%@ %ld", [self getCellIdentifierPrefix], (long)[self.promotion presentableLinksCount]];
return ident;
}


 - (NSString *)getCellIdentifierPrefix
 {
     NSString *cellIdentifier = TOPREUSEPREFIX;

     switch (self.promotion.imagePos)
     {
         case left:
             cellIdentifier = LEFTREUSEPREFIX;
             break;
         case right:
             cellIdentifier = RIGHTREUSEPREFIX;
             break;
         case top:
             cellIdentifier = TOPREUSEPREFIX;
             break;
         case bottom:
             cellIdentifier = BOTTOMREUSEPREFIX;
             break;
         default:
             break;
     }
     return cellIdentifier;
 }

 - (NSString *)getXibIdentifier
 {
     NSString *xibIndentifier = @"PromoCardImageTop";

     switch (self.promotion.imagePos)
     {
         case left:
             xibIndentifier = LEFTCELLXIB;
             break;
         case right:
             xibIndentifier = RIGHTCELLXIB;
             break;
         case top:
             xibIndentifier = TOPCELLXIB;
             break;
         case bottom:
             xibIndentifier = BOTTOMCELLXIB;
             break;
         default:
             break;
     }
     return xibIndentifier;
 }


 - (id)getCell4:(UITableView*)tableView
 {
     UINib *nib = [UINib nibWithNibName:[self getXibIdentifier] bundle:nil];
     NSString *cid = [self getCellIdentifier];
     [tableView registerNib:nib forCellReuseIdentifier:cid];

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cid];
     NSAssert(cell, @"standard dequeue mechanism must work, fix this");
     return cell;
 }

晚了。但它在运行时并不能很好地工作:

原因:'笔尖(PromoCardImageTop)中的单元重用标识符与用于注册笔尖的标识符(PromoCardImageTop 1)不匹配'

任何想法如何处理这个?

确实存在

  • (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // 较新的出队方法保证单元格被返回并正确调整大小,假设标识符已注册

在 ios 6 中添加提示有一些机制可以完成 我正在努力实现的目标。

类似于这个问题:

Xcode 4.2 "cell reuse indentifier in nib (Cell) does not match the identifier used to register the nib (ThisCell)"

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    解决方案相当难看:

    registerClass:forCellReuseIdentifier:
    

    必须用来代替

    registerNib: forCellReuseIdentifier:
    

    然后你会有一个相当丑陋的构造函数浪费来自 [super initWithStyle:style reuseIdentifier:reuseIdentifier] 的对象,因此不能移植到 swift:

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier
    {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        NSArray *ugly = [reuseIdentifier componentsSeparatedByString:@" "];
        NSString *cid = ugly[0];
        NSString *xibid = whateverfallbackis;
        if([cid isEqual:LEFTREUSEPREFIX]) {
            xibid = LEFTCELLXIB;
        } else
        if([cid isEqual:RIGHTREUSEPREFIX]) {
            xibid = RIGHTCELLXIB;
        } else
        if([cid isEqual:TOPREUSEPREFIX]) {
            xibid = TOPCELLXIB;
        } else
        if([cid isEqual:BOTTOMREUSEPREFIX]) {
            xibid = BOTTOMCELLXIB;
        } else {
            NSAssert(0, @"implement");
        }
        NSArray *cells = [[NSBundle mainBundle] loadNibNamed:xibid owner:self options:nil];
        return cells.firstObject;
    }
    return self;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2014-08-02
      • 2021-08-14
      相关资源
      最近更新 更多