2012-01-12 57 views
1

我与indexPath.sectioncellForRowAtIndexPath:方法一个奇怪的问题。怪异的行为“的cellForRowAtIndexPath:”

我有4段分组的实现代码如下,我尝试应用自定义UITableViewCell为第3,但它不工作。

当我尝试if(indexPath.section==0){...}它的工作(并为section==1section==2以及),但它失败section==3。 (?)

我不知道为什么,那是没有意义的..是不是有人已经有了这样(怪)的问题?

当我尝试if(indexPath.row==0){...}它适用于所有的4个部分..所以..?!

这里是我的代码:

//ViewController.h 
import "DirectionsTableViewCell.h" 
DirectionsTableViewCell *directionsCell; // customized UITableViewCell 

//ViewController.m 
if (indexPath.section==3) { 
     static NSString *CellIdentifier = @"directionsCell"; 

     DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil]; 
      cell = directionsCell; 
     } 

     return cell; 
    } 
    else { 
     static NSString *CellIdentifier = @"defaultCell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     cell.textLabel.text = @"Test"; 

     return cell; 
    } 


问题解决了!

我只是说if(indexPath.row)并能正常工作。

最后你得到这个:

if(indexPath.section==3) { 
    if(indexPath.row) { 
     static NSString *CellIdentifier = @"directionsCell"; 

     DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil]; 
      cell = directionsCell; 
     } 

     return cell; 
    } 
} 
+0

在何种意义上失败?你得到什么错误或意外的结果? – PengOne 2012-01-12 23:13:48

+0

我其实有这个问题。你可以发布你的'cellForRowAtIndexPath'方法吗?这可能有助于确认我的预感。 – 2012-01-12 23:16:01

+0

好吧,我没有任何错误,但它不加载自定义UITableViewCell ..我仍然有默认的。 – bs7 2012-01-12 23:16:59

回答

1

好吧,你永远你if(cell == nil)内部分配DirectionsTableViewCell。

在代码中的这一部分:

DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil]; 
      cell = directionsCell; 
     } 

你永远不分配DirectionsTableViewCell类型,它稍后重新使用的电池。我还注意到你有一个名为directionsCell的伊娃,其型号为DirectionsTableViewCell。除非你分配和设置它在其他地方,cell = directionsCell最终分配零对象添加到您cell

试试这个代码,而不是看它是否工作:

static NSString *CellIdentifier = @"directionsCell"; 

directionsCell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(directionsCell == nil) { 
     directionsCell = [[DirectionsTableViewCell alloc] init]; //Or whatever your initializer is 
    } 

    return directionsCell; 
+0

我要定势,多亏 – bs7 2012-01-12 23:54:06

+0

嗯,谢谢你那工作只是不得不添加'[一个NSBundle mainBundle] loadNibNamed:@ “DirectionsTableViewCell” 老板:自我选择:无]。!!!' line。谢谢!Sid! – bs7 2012-01-13 00:03:09

+0

不客气:) – Sid 2012-01-13 00:08:57