2011-01-21 35 views
0

有一個想法來解決這個問題?問題cellForRowAtIndexPath和NSBundle mainBundle

這是respectly的正確和錯誤的方法
alt textalt text

這是工作代碼:

- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    @try { 
     newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]); 
     if (newsRow == nil) { 
      if ([[Device GetModel] isEqualToString:@"iPad Simulator"] || 
       [[Device GetModel] isEqualToString:@"iPad"]) 
       [[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil]; 
      else [[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil]; 

      if ([tableArray count] > 0) 
       [newsRow setData:[tableArray objectAtIndex:indexPath.row]]; 
     }  
    } 
    @catch (NSException * e) { 
     NSLog(@"a!!!!!"); 
    } 
    return newsRow; 
} 

...但因爲在我的3G手機是慢慢當我滾動表上/下我改變了這種方式的代碼,插入設備檢查viewDidLoad並通過它在cellForRowAtIndexPath

NSArray *cRow; 
@property (nonatomic, retain) NSArray *cRow; 
[...] 
@syntetize cRow; 

- (void)viewDidLoad { 
[...] 
    if ([[Device GetModel] isEqualToString:@"iPad Simulator"] 
     || 
     [[Device GetModel] isEqualToString:@"iPad"]) 
     cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil] retain]; 
     else cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil] retain]; 
[...] 
} 

- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    @try { 
     newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]); 
     if (newsRow == nil) { 

      // 
      // ADDED CODE. 
      // 
      newsRow = [cRow objectAtIndex:0];  
      // 

      if ([tableArray count] > 0) 
       [newsRow setData:[tableArray objectAtIndex:indexPath.row]]; 
     }  
    } 
    @catch (NSException * e) { 
     NSLog(@"a!!!!!"); 
    } 
    return newsRow; 
} 

現在表格只顯示一行。如果我滾動,行更改,但它只顯示一行...
alt text
有什麼問題?

有幫助嗎?

感謝,
一個

回答

1

在當你調用dequeueReusableCellWithIdentifier這種情況下:您將獲得零回來,因爲沒有可以離隊細胞。每當你得到一個零單元格時,你需要加載nib,否則你不會在第一個單元格後面添加任何單元格。

如果調用GetModel的速度很慢,您可以在viewDidLoad中調用它並將其存儲以備後用。

由於您在每個單元格中繪製的視圖數量太大,您的表格可能無法平滑滾動。如果您的背景始終是純色,則應確保所有視圖添加到單元格或不透明。如果你需要一個圖像或漸變背景,那麼你應該看看在一個視圖中繪製單元格。蘋果有一個很好的例子來說明如何在這裏做到這一點:TableViewSuite。特別檢出TimeZoneView文件。

+0

好的,我該如何解決問題? – elp

+0

您可以開始將加載筆尖的代碼移回tableView:cellForRowAtIndexPath :,如果您在iPad上沒有使用viewDidLoad,則可以存儲BOOL。如果在此之後滾動仍然很慢,而不是在nib中創建的單元格,則可以像Apple那樣在您的示例中創建您自己的UITableViewCell的子類。 – criscokid

+0

完美。有沒有辦法在一個var中設置[[NSBundle mainBundle] loa ...]來在循環中使用? – elp

相關問題