When attempting to load a custom UITableViewCell from a nib, I am unable to get the cell to appear only if I have set the reuseIdentifier on the prototype cell in the main storyboard. If this is set to blank, I get a warning from xcode, however the code runs correctly. I have similar code in several other controllers, however this is the only one presenting this issue.
- (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;
}
It looks like you configure each cell only in the case where the tableView cannot dequeue a cell for you. For one thing, your cell reuse is probably broken as a result. So that's one issue.
And then in the case of the storyboard, if the cell identifier is set correctly then the [tableview dequeueReusableCellWithIdentifier:] will always return a cell for you. So assetTableCell will not be nil, even the first time.
It's not clear exactly what all your logic is here, but you need to be able to properly set the content of your cell in the case where assetTableCell is not nil. (either because the cell is being recycled or because the cell came from the storyboard cache)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With