2012-12-07 45 views
1

感谢您的帮助。 我有一个自定义单元格,使用下面的代码进行展开。但是,第一个单元格(索引0)总是在ViewControllers启动时扩展?iOS自定义单元格可扩展 - 索引0问题

我错过了什么?你如何在启动时将它们全部展开并仅在选择时展开。

很多谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     CustomCellCell *cell; 
     static NSString *[email protected]"myCustomCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

     if (cell == nil) 
     { 
      NSArray *test = [[NSBundle mainBundle]loadNibNamed:@"myCustomCell" owner:nil options:nil]; 
      if([test count]>0) 
      { 
       for(id someObject in test) 
       { 
        if ([someObject isKindOfClass:[CustomCellCell class]]) { 
         cell=someObject; 
         break; 
        } 
       } 
      } 
     } 

     cell.LableCell.text = [testArray objectAtIndex:[indexPath row]]; 
     NSLog(@"data testarray table %@", [testArray objectAtIndex:[indexPath row]]); 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     return cell; 
    } 

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     self.selectedRow = indexPath.row; 
     CustomCellCell *cell = (CustomCellCell *)[tableView cellForRowAtIndexPath:indexPath]; 

     [tableView beginUpdates]; 
     [tableView endUpdates]; 

     cell.buttonCell.hidden = NO; 
     cell.textLabel.hidden = NO; 
     cell.textfiledCell.hidden = NO; 
     cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
     cell.clipsToBounds = YES; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
     if(selectedRow == indexPath.row) { 
      return 175; 
     } 

     return 44; 
    } 

回答

1

这是因为默认值selectedRow为零。您需要将其初始化为:

selectedRow = NSIntegerMax; //or selectedRow = -1; 

或其他一些默认值。您可以在viewDidLoad方法左右添加此项。每当你声明一个int型变量时,它的默认值是零。所以如果你有一个场景,例如在上面的例子中需要检查零,你应该默认它不会被使用的值。负值或NSIntegerMax可以用于此。

+0

我觉得自己像一个ideeoat ....感谢您的帮助。 – AhabLives

+0

@AhabLives,不是问题。通常人们往往会错过这一点。请接受,如果这有帮助。 :) – iDev

+1

我试过......告诉我等10分钟.....所以我等着......再看看 – AhabLives

0

我猜selectedRow是一个整数实例变量。该整数的起始值为0.由于第一个表格单元格是第0行,即使您没有故意设置它,它仍与selectedRow匹配。

解决此问题的一种方法是将selectedRow存储为NSIndexPath而不是整数。

然后,你可能只是这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if([selectedRow isEqual:indexPath]) { 
     return 175; 
    } 
    return 44; 
} 

而且由于selectedRow将默认为零,你不会得到一个错误的比赛。如果您稍后决定使用部分,它也更加灵活。

相关问题