2011-07-20 42 views
0
if (indexPath.row % 2 == 0) { 
    // EVEN 
    cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease]; 
     UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 

     UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                blue: (241.0/255.f) alpha: 1.0]; 
     bg.backgroundColor = colour; 
     cell.backgroundView = bg; 
     cell.textLabel.backgroundColor = bg.backgroundColor; 
     [bg release]; 
     cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

} else { 
    // ODD 

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease]; 
     UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 

     UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                blue: (180.0/255.f) alpha: 1.0]; 
     bg.backgroundColor = colour; 
     cell.backgroundView = bg; 
     cell.textLabel.backgroundColor = bg.backgroundColor; 
     [bg release]; 
     cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    } 
} 

return cell; 
} 

這是我的自定義單元格。第一個是第二個單元格是odd's.My數組有194個元素,但是當我運行應用程序時,我只能看到10個第10個元素之後它再次返回第一個元素給我相同的10個有人可以告訴我這裏有什麼問題嗎?我如何命名單元格導航控制器?

+0

沒有錯在這裏......'numberOfRows'返回什麼?錯誤必須在那裏 – 2011-07-20 13:16:38

+0

我檢查了項目數組NSLog(@「%d」,[items count]);它給了我194我從URL讀取它們,並給我真實的數字,我怎麼看行數 –

+0

是這個方法返回的數字? – 2011-07-20 13:20:34

回答

2

試試這個。

if (indexPath.row % 2 == 0) { 
// EVEN 
     cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease]; 
      UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 


     UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                blue: (241.0/255.f) alpha: 1.0]; 
     bg.backgroundColor = colour; 
     cell.backgroundView = bg; 
     cell.textLabel.backgroundColor = bg.backgroundColor; 
     [bg release]; 
    } 
     cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


} else { 
    // ODD 

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease]; 
     UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 

     UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                blue: (180.0/255.f) alpha: 1.0]; 
     bg.backgroundColor = colour; 
     cell.backgroundView = bg; 
     cell.textLabel.backgroundColor = bg.backgroundColor; 
     [bg release]; 
    } 
     cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


    } 

    return cell; 
} 
+0

非常感謝現在它正在工作 –

1

爲了讓您的視圖顯示正確的行數,請確保-(NSUInteger)tableView:numberOfRowsInSection:(NSUInteger)section返回正確的數字。 (在你的情況下,你應該返回[items count])。

+0

感謝vince爲您的耐心和幫助lanc解決方案是真實的。 –

+0

好了解... – 2011-07-20 13:27:40

+0

我把cell.textLabel.text = [items objectAtIndex:indexPath.row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;代碼到if語句中。他拿出那部分。 –

相關問題