0

サーバー上に、tableView に表示したいデータがあります。

問題は、カテゴリに基づいてデータを表示したいので、セクションタイトルとなるカテゴリを持つ配列カテゴリがあり、その中にデータがあるため、セクションにデータを表示するには、配列を宣言する必要があることです。

たとえば、3 つのカテゴリがある場合、データを入力するために 3 つの配列を作成する必要がありますが、カテゴリが動的でサーバーから取得されるため、より多くのカテゴリがある場合はどうなりますか。

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [categoryArray count] ;
    }

そして、セクションのタイトルにタイトルを設定する方法は、カテゴリの配列にあるため、セクションごとに配列の場合です。

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


    NSLog(@"Number of Sections");
    if(section == 0)
     return @"Sales";
    if(section == 1)
    return @"Soft Skills";
}

tableView セルにデータを表示するには、すべてのカテゴリの配列を作成できますか?

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


if (section==0)
{

    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

    int count=[resultArray count];

    NSLog(@"resultArry Row Counts is %d",count);

    return [resultArray count];

}
else{
    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];

    int count=[resultArrayOne count];

    NSLog(@"resultArry Row Counts is %d",count);

    return [resultArrayOne count];
}
     }




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

NSLog(@"Table Cell Data");


     static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


if (indexPath.section==0) {



    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];


    ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
    NSString *cellValue =theCellData.sub_Category;
    cell.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
    NSLog(@"Cell Values %@",cellValue);
    cell.textLabel.text = cellValue;

    return cell;
}

else {



    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
    ObjectData *theCellData = [resultArrayOne objectAtIndex:indexPath.row];
    NSString *cellValue =theCellData.sub_Category;
    cell.font=[UIFont fontWithName:@"Helvetical Bold" size:14];

    NSLog(@"Cell Values %@",cellValue);
    cell.textLabel.text = cellValue;
    return cell;



   }
      }
4

3 に答える 3

1

サーバーからカテゴリを取得することはあなたの質問ではないように思われるので、視覚化を改善するために、事前に入力された配列に基づいて回答します。

NSMutableArray *categories = @[@"Cat1", @"Cat2"];

// creata a dictionary with all the array for the categorie rows
NSMutableDictionary *rowDict = @{@"Cat1":@[@"Cat1Row1",@"Cat1Row2",@"..."],
                     @"Cat2":@[@"Cat2Row1", @"Cat2Row2",@"..."]

このソリューションの鍵は、カテゴリ文字列を辞書のキーとして使用することです。

このようにタイトルにアクセスできるようになりました

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return categories[section];
}

そして、このように行にアクセスします

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // ...
  // create or deque a cell like you normally would do

  // now configure the cell
  cell.textLable.text = [rowDict[categories[indexPath.section]] objectAtIndex:indexPath.row] 
}
于 2013-03-26T06:55:26.620 に答える
0

以下のコードを試してください。それはあなたの問題を解決します:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
      return [categoryArray objectAtIndex:section];
}

編集 :

  1. まず、カテゴリのデータを格納するモデル データ クラスを作成します。
  2. このモデル クラスを使用して、機能numberOfRowsInSectioncellForRowAtIndexPathデリゲートします。
  3. カテゴリごとに異なる配列を作成する代わりに。この配列を 1 つのモデル クラスに格納します。扱いやすいでしょう。
于 2013-03-26T06:04:51.243 に答える
0

使用する

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return [categoryArray objectAtindex:section];
}

セクションのタイトル。

同様に、各カテゴリの値を nsmutable 配列 NSDictionary に格納し、各カテゴリのデータを uitableviewcell に表示します。

于 2013-03-26T06:05:01.307 に答える