Reputation: 2923
I need to make a object in the beginning of the cellForRowAtIndexPath
to add different cells in it with switch section:
switch (indexPath.section) {
case DetailControllerAddressSection: {
NSString *address = [self addressText];
UITableViewCell *cell;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (IS_OS_7_OR_LATER) {
cell = (CustomDetailCell *) [tableView dequeueReusableCellWithIdentifier:@"AddressCell" forIndexPath:indexPath];
cell.mainLabel.text = address;
cell.detailLabel.text = [self distanceMessageForObjectData:self.objectData];
} else {
UniversalAddressCell *cell = (UniversalAddressCell *) [tableView dequeueReusableCellWithIdentifier:@"UniversalAddressCell" forIndexPath:indexPath];
cell.backgroundView = [self cellBackgroundImageView:indexPath];
cell.mainLabel.text = address;
...
But in this case the cell is the UITableViewCell
and I can't get the labels from CustomDetailCell
class. How to solve this? The decision is simple I believe but I don't know how to solve it..
Upvotes: 0
Views: 247
Reputation: 4561
If you have two different NIBs for two cells:
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
switch (indexPath.section) {
case DetailControllerAddressSection:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell1" owner:self options:nil];
cell1 = [nib objectAtIndex:0];
// write here cell1 specific
cell=cell1;
break;
case anotherCase:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell2" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
// write here cell2 specific
cell=cell2;
}
}
return cell;
Upvotes: 1
Reputation: 1840
The problem is with this: UITableViewCell *cell;
Even though you cast the cell as (CustomDetailCell *)
the storage type is still UITableViewCell
what you could do is:
switch (indexPath.section) {
case DetailControllerAddressSection: {
NSString *address = [self addressText];
UITableViewCell *cell;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (IS_OS_7_OR_LATER) {
CustomDetailCell *detailCell = (CustomDetailCell *) [tableView dequeueReusableCellWithIdentifier:@"AddressCell" forIndexPath:indexPath];
detailCell.mainLabel.text = address;
detailCell.detailLabel.text = [self distanceMessageForObjectData:self.objectData];
cell = detailCell;
} else {
UniversalAddressCell *universalCell = (UniversalAddressCell *) [tableView dequeueReusableCellWithIdentifier:@"UniversalAddressCell" forIndexPath:indexPath];
universalCell.backgroundView = [self cellBackgroundImageView:indexPath];
universalCell.mainLabel.text = address;
cell = universalCell;
Upvotes: 1
Reputation: 2306
I guess you'd do typecasting..
[(CustomDetailCell *)cell mainLabel].text = address;
Upvotes: 1