当尝试从笔尖加载自定义UITableViewCell时,只有在主故事板中的原型单元格上设置了reuseIdentifier,我才能让单元格出现。如果将其设置为空白,我会收到来自xcode的警告,但代码运行正常。我在其他几个控制器中有类似的代码,但是这是唯一一个提出这个问题的控制器。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"AssetsTableCell";
AssetsTableCell *assetTableCell = (AssetsTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
int rightIndex = (indexPath.row * 4);
int rightMiddleIndex = (indexPath.row * 4) + 1;
int leftMiddleIndex = (indexPath.row * 4) + 2;
int leftIndex = (indexPath.row * 4) + 3;
if (assetTableCell == nil) {
NSArray *topLevelObjects;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AssetsTableCell_iPhone" owner:nil options:nil];
assetTableCell = (AssetsTableCell *)[topLevelObjects objectAtIndex:0];
[assetTableCell setDelegate:self];
}
// Need to collect the badge details in groups of 4.
@try {
NSArray *rowArray = [NSArray arrayWithObjects:[remoteContainers objectAtIndex:rightIndex],
[remoteContainers objectAtIndex:rightMiddleIndex],
[remoteContainers objectAtIndex:leftMiddleIndex],
[remoteContainers objectAtIndex:leftIndex],
nil];
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:
[rowArray objectAtIndex:0], @"LEFT_CONTAINER",
[rowArray objectAtIndex:1], @"LEFT_MIDDLE_CONTAINER",
[rowArray objectAtIndex:2], @"RIGHT_MIDDLE_CONTAINER",
[rowArray objectAtIndex:3], @"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
@catch (NSException *exception) {
DLog(@"Exceeds boundary of remoteContainers array by %d.", ([remoteContainers count] % 4));
int remainder = [remoteContainers count] % 4;
switch (remainder) {
case 1: {
// Check to see the number of remaining containers left in the rest of the array. This dictates the way the final row must be constructed.
if ([remoteContainers objectAtIndex:currentStartRangeIndex]) {
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", @"EMPTY_CELL", @"LEFT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
}
break;
case 2: {
// There are two elements at the end of the array.
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], @"LEFT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
break;
case 3: {
// There are three elements at the end of the array.
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], @"LEFT_MIDDLE_CONTAINER", [remoteContainers objectAtIndex:leftMiddleIndex], @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
break;
default: {
DLog(@"Thought the array had more elements, however was unable to determine the number of elements remaining in array.");
}
break;
}
}
}
return assetTableCell;
}
答案 0 :(得分:2)
看起来您只在tableView无法为您出列单元格的情况下配置每个单元格。首先,您的单元重用可能因此而被破坏。这是一个问题。
然后在故事板的情况下,如果单元标识符设置正确,那么[tableview dequeueReusableCellWithIdentifier:]将始终为您返回一个单元格。所以assetTableCell不会是零,即使是第一次。
目前还不清楚你的所有逻辑是什么,但是在assetTableCell不为零的情况下,你需要能够正确设置你的单元格的内容。 (或者因为单元格正在被回收,或者因为单元格来自故事板缓存)