2012-03-01 43 views
0

我開發了一個應用程序,其中我從核心數據中獲取數據,並將其顯示在表視圖中。我一切正常,直到獲取數據,但將它們插入表視圖時,只有最後一個條目顯示在表視圖中。以下是我寫的代碼請預覽並幫助我獲得解決方案。插入數據到ios中的表視圖問題

Deviceinfo *app = [arr objectAtIndex:indexPath.row]; 
switch(indexPath.row) 
{ 


case 0: 
     NSLog(@"%@",app.platform); 
     cell.textLabel.text = @"platform"; 
     cell.detailTextLabel.text =[app platform]; 

case 1: 
     NSLog(@"%@",app.model); 
     cell.textLabel.text = @"model"; 
     cell.detailTextLabel.text = [app model]; 
case 2: 
     NSLog(@"%@",app.mac_address); 
     cell.textLabel.text = @"mac address"; 
     cell.detailTextLabel.text = [app mac_address]; 
} 
return cell; 

我在cellForRowAtIndexpath委託中實現了這段代碼。我只獲取表格視圖中的mac地址。希望能爲更好的解決方案

感謝

回答

1

插入break語句每種情況後,否則情況會剛好落在雖然到下一個

switch(x) { 
    case 1: 
     break; 
    default: 
     break; 
} 

根據您的補充意見,你可以試試下面的:每個設備都有自己的表格視圖部分,每個部分將有3個表格視圖行,每個信息一個。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return devices.count 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 3; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     cell = [[FEMenuItemInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];    
    }  

    Device *device = [devices objectAtIndex:indexPath.section]; 

    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"platform"; 
      cell.detailTextLabel.text = [device platform]; 
      break;    
     case 1: 
      cell.textLabel.text = @"model"; 
      cell.detailTextLabel.text = [device mac_address]; 
      break; 
     case 2: 
      cell.textLabel.text = @"mac address"; 
      cell.detailTextLabel.text = [device mac_address]; 
      break;       
    } 

    return cell; 
} 
+0

但我需要所有這三種情況下執行。如果我插入中斷後執行第一個案件控制退出循環權利如何執行其餘案件 – NNR 2012-03-01 08:04:34

+0

我需要所有案件應執行並顯示其內容在不同的單元格。 – NNR 2012-03-01 08:34:31

+0

您需要在多行上使用多個單元格。等一下,我會用適當的代碼更新它。 – richerd 2012-03-02 03:21:20