0

在 Big Nerd Ranch Book:— iOS Programming (3rd ed.) 中,在 Ch9 page-205 中有一个青铜挑战。我们必须显示 2 个部分,1 个显示项目> 和其他带有项目<=
我知道如何解决这个挑战使用两个单独的数组,一个存储昂贵的物品,另一个存储便宜的物品。但我不想那样解决这个问题。我想要两种不同类型的单元格,其中 type1 指向昂贵的单元格,并在 section0 中进行了调整。type2 指向更便宜的并被放置在 section1 中。我已经尝试过了,但没有任何成功。这是代码:- 这是用于配置每个部分中的行数:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      //#warning Incomplete method implementation.
      // Return the number of rows in the section.
      int numberOfRowsWithValueLessThanOrEqualToFifty=0, numberOfRowsWithValueMoreThanFifty=0;
       NSLog(@"No. of BNRItems- %d", [[[BNRItemStore sharedStore] allItems] count]);
       for (BNRItem *item in [[BNRItemStore sharedStore] allItems])
       {
            //BNRItem *p=[[[BNRItemStore sharedStore] allItems] objectAtIndex:i];
            if ([item valueInDollars]>50) 
            {
                 numberOfRowsWithValueMoreThanFifty++;
            }
            else
            {
                 numberOfRowsWithValueLessThanOrEqualToFifty++;
            }
       }

       if (section==0) {
            return numberOfRowsWithValueMoreThanFifty; //no. of rows in section0
       }
       else{
           return numberOfRowsWithValueLessThanOrEqualToFifty;//no. of rows in section1
       }

     }

这是用于配置两个单元格并根据特定情况返回其中一个:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
   {
        NSLog(@"hello");

        static NSString *CellIdentifier = @"Cell";
        static NSString *CellIdentifier2 = @"Cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        //Configure the cell...
        if (!cell) {
            cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        if (!cell2) {
            cell2= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault           reuseIdentifier:CellIdentifier2];
        }

        BNRItem *p= [[[BNRItemStore sharedStore] allItems] objectAtIndex:indexPath.row];
        if([p value] >50)
        {
            [[cell textLabel] setText:[p description]];
        }
        else{
            [[cell2 textLabel] setText:[p description]];
        }

        if (indexPath.section==0) {
            return cell;
        }
        else{
            return cell2;
        } 
 }

现在代码有很多问题。如果第一个if子句与第二个else子句一起运行,那么选择 section0 和 section1 中的内容的整个目的就会被破坏。在上面的代码中,唯一有效的情况是 2 个if子句一起运行或 2 个else时子句一起运行。但显然这不会发生,因为控制流可能是随机的。那么这是否意味着一个人不能使用两种不同类型的单元格来解决这个挑战 2 个不同的部分,而是必须创建单独的数组来首先存储便宜和昂贵的物品,然后适当地将它们推到 section0 和 section1 中?请。建议因为这种方式是我解决这个问题的第一个想法,我真的希望以这种方式完成。

4

1 回答 1

0

为什么不先检查价格,然后将相应的表格单元格出列(或创建)?像这样:

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
NSLog(@"hello");
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
UITableViewCell* cell = nil;
NRItem *p= [[[BNRItemStore sharedStore] allItems] objectAtIndex:indexPath.row];
if (indexPath.section==0)
{
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [[cell textLabel] setText:[p description]];
}
else
{
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
    }
    [[cell textLabel] setText:[p description]];
}
return cell;
}
于 2013-11-07T12:13:28.233 回答