0

Big Nerd Ranch Book:— iOS Programming (3rd ed.) では、Ch9 page-205 にブロンズ チャレンジがあります
。 1 つは高価なアイテムを格納し、もう 1 つは安価なアイテムを格納します。しかし、私はこの問題をそのように解決したくありませんでした。type1 が高価なものを指し、section0 で調整される 2 つの異なるタイプのセルが必要です。そして、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
       }

     }

これは、2 つのセルを構成し、特定のケースに基づいていずれかを返すためのものです。

   - (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句が 2 番目のelse句と一緒に実行される場合、セクション 0 とセクション 1 に何が入るかを選択するという目的全体が破棄されます。上記のコードで唯一有効なケースは、2 つのif句が一緒に実行される場合、または 2 つの elseの場合です。句は一緒に実行されます。しかし、制御フローがランダムになる可能性があるため、明らかにそれは起こりません。つまり、2 つの異なるセクションに 2 つの異なるタイプのセルを使用してこの課題を解決することはできず、最初に安価なアイテムと高価なアイテムを格納するために別々の配列を作成し、次にそれらをセクション 0 とセクション 1 に適切に押し込む必要があるということですか?? お願いします。この方法のアドバイスは、この問題を解決するための私の最初のアイデアであり、本当にこの方法で実行したいと考えています。

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 に答える