2011-08-14 255 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