2011-08-14 253 views
0

我的程序有2個NSMutableArray(s),每個包含一個項目列表。一些成本超過50美元,一些成本低於50美元。任務是將這些信息顯示爲表格的一部分。所以..Objective-C,UITableView,需要澄清

我的表有2個部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

每個部分都有一個名字

- (NSString *)tableView:(UITableView *) 
    tableView titleForHeaderInSection:(NSInteger)section { 

    NSString *lSectionTitle; 
    switch (section) { 
     case 0: 
      lSectionTitle = @"Under $50"; 
      break; 
     case 1: 
      ... 

每一部分都有一個計數

-(NSInteger) tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {  
    NSInteger count= 0; 

    switch (section) { 
     case 0: 
      count = [[[PossessionStore defaultStore] under50Possessions] count]; 
      break; 
     case 1: 
      ..... 

然後終於我們弄清楚什麼作爲給定單元的一部分顯示

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    if (!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]autorelease]; 
    } 

    Possession *p = [[[PossessionStore defaultStore] allPossessions] objectAtIndex:[indexPath row]]; 

    [[cell textLabel] setText:[p description]]; 

    return cell; 
} 

上面的代碼代碼allPossessions,其中包含兩個項目超過50美元和50美元以下。我被困在這一點上。

在這個例子中,有沒有一種方法可以讓我知道我是否被要求爲低於或超過$ 50的類別繪製單元格?

回答

1

一切都很好,但我真的不明白UITtable「知道」 如何將物品放置在正確的類別下並超過$ 50。

它沒有。它看起來像你可能只是幸運的,因爲PosessionStore從-allPossessions返回數據的方式擁有以價格或其他方式組織的財產。在填充每個單元格時,您的-tableView:cellForRowAtIndexPath:方法應該真正考慮該部分以及該行。

+0

正是!在這個時候我正在讀這個,我看到[indexPath部分]可以被查詢。謝謝迦勒! – JAM