2014-02-17 74 views
0

我需要一个对象在cellForRowAtIndexPath年初在其添加不同的细胞开关部分:如何开关部分使用自定义的类实例化的UITableViewCell

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; 
... 

但是,在这种情况下,细胞是UITableViewCell,我无法从CustomDetailCell类中获得标签。如何解决这个问题?这个决定很简单,我相信,但我不知道如何解决它..

回答

1

的问题是这个:UITableViewCell *cell;

即使虽然你施放该小区作为(CustomDetailCell *)存储类型仍然是UITableViewCell

你可以做的是:

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; 
1

我猜你会做类型转换..

[(CustomDetailCell *)cell mainLabel].text = address; 
1

如果您有两个不同的NIB用于两个单元格:

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; 
相关问题