我有一些单元格在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;
}
迟。但它在运行时并不常用:
原因:'nib中的单元重用标识符(PromoCardImageTop)与用于注册nib的标识符(PromoCardImageTop 1)不匹配'
有任何想法如何解决这个问题?
有
的事实在ios 6中添加了暗示,有一些机制可以实现 我想要实现的目标。
与此问题类似:
答案 0 :(得分:0)
解决方案相当丑陋:
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;
}