2013-12-09 59 views
0

我有一個包含多個部分的表視圖。當我將數據添加到plist中時,它們會添加到正確的部分,但是當表格顯示時它看起來像這樣;在每個部分中重複數據的表視圖

表視圖:

Section 1 
    Note 1 

Section 2 
    Note 1 
    Note 2 

,而不是注1:在部分之一,並注2:在第2節

我相信它在celForRowAtIndexPath其中的錯誤是,但我不知道是什麼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
cell.textLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
cell.detailTextLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:@"Date"]; 
}  

更新 - NSLog的自我,指出:

{ Category = Section 1; 
Date = "December 9, 2013"; 
Text = "Note 1 text"; 
Title = "Note 1"; }, 

{ Category = Section 2; 
Date = "December 9, 2013"; 
Text = "Note 2 text"; 
Title = "Note 2"; 
} 

的plist鏈接:

http://i.stack.imgur.com/qrbfh.png

+0

的'self.notes'陣表演NSLog的。 –

+0

您的self.notes數據的外觀如何? – Greg

+0

self.notes與我期望的完全一致 - 請參閱更新 – user3001526

回答

0

嘗試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath{ 
     if (indexPath.section == 0){ 
      NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"]; 
      NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred]; 
      cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
      cell.detailTextLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Date"]; 

     } else if (indexPath.section == 1){ 
      NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"]; 
      NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred]; 
      cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
      cell.detailTextLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Date"]; 
     } 
    } 

最後NSPredictate參數,例如:@「1類」,應該如果你想這取決於用戶通過您的應用程序邏輯是控制器。

+0

我沒有想到這一點,非常感謝 – user3001526

0

採取部分顧及的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ 

    if (indexPath.section == 0){ 
     cell.textLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
     cell.detailTextLabel.text = [[self.notes objectAtIndex:indexPath.row]  objectForKey:@"Date"]; 

    } else if (indexPath.section == 1){ 
    // do stuff speficic for section 1 (the second section) 
    // ... 

    } 
} 
+0

嘿,感謝您的答覆,但2部分只是一個例子,部分的數量可以根據用戶改變,所以硬編碼每個部分的條件將很難 – user3001526

+0

@ user3001526 - 你的'notes'對象應該是一個數組數組。外部數組每個部分包含一個條目,而內部數組包含單個部分的條目。 –

+0

這有點讓我困惑,請你解釋一下嗎? – user3001526

1

如果你想在表格部分顯示數據,那麼你的數組結構應該是這樣的。 然後不需要任何硬編碼的比較。

( 
    (
    {key value pair}, 
    {key value pair} 
), 
    (
    {key value pair}, 
    {key value pair} 
), 
    ( 
    {key value pair}, 
    {key value pair}, 
    {key value pair}, 
    {key value pair}, 
    {key value pair} 
) 
) 

那麼在這樣的方式訪問:

cell.textLabel.text = [[[self.notes objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]objectForKey:@"Title"]; 
相關問題