2012-01-12 89 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