38

複数のセクションでtableViewを作成したいのですが、辞書を使用したくありません.2つの配列があります.最初の配列を最初のセクションにロードし、2番目の配列を2番目のセクションにロードする必要があります.

3つのアイテムを持つarrayOneと4つのアイテムを持つarrayTwoがあるので、それらを追加してセクションに表示する方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if(section == 0)
        return resultArray.count;
    else
        return resultArray.count;
}

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


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSLog(@"Number of Sections");
    if(section == 0)
        return @"Section 1";
    if(section == 1)
        return @"Section 2";
}

- (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];
     }

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

     if (indexPath.section==0) {    
         appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
         ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
         NSString *cellValue =theCellData.category;
         NSLog(@"Cell Values %@",cellValue);
         cell.textLabel.text = cellValue;
         return cell;
     }
     else {
         ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
         NSString *cellValue =theCellData.category;
         cell.textLabel.text = cellValue;
         return cell;       
     }
}
4

6 に答える 6

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

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if (section==0)
      {
             return [array1 count];
      }
      else{
             return [array2 count];
      }
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      if(section == 0)
           return @"Section 1";
      else
           return @"Section 2";
 }


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

      static NSString *CellIdentifier = @"Cell";

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

 if (indexPath.section==0) {
     ObjectData *theCellData = [array1 objectAtIndex:indexPath.row];
     NSString *cellValue =theCellData.category;
     cell.textLabel.text = cellValue;
 }
 else {
     ObjectData *theCellData = [array2 objectAtIndex:indexPath.row];
     NSString *cellValue =theCellData.category;
     cell.textLabel.text = cellValue;
 }
     return cell;        
 }
于 2013-03-01T05:28:07.233 に答える
0

これを変えるif (section==0)

if (indexPath.section==0)

この関数を実装する必要があります:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
     if(section == 0)
         return arrSection1.count;
      else
         return arrSection2.count;
 }
于 2013-03-01T05:22:43.513 に答える
0

あなたnumberOfRowsInSectionは次のようになるはずです:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return firstArray.count;
    } else {
        return secondArray.count;
    }
}
于 2013-03-01T05:23:44.177 に答える
0

あなたはほぼ正しい軌道に乗っています....

arrOneあなたの2つの配列がであると考えてみましょうarrTwo.....

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if(section == 0)
      return [arrOne count];
      else
      return [arrTwo count];
    }

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

         static NSString *CellIdentifier = @"Cell";

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

             if (indexPath.section==0) {


        ObjectData *theCellData = [arrOne objectAtIndex:indexPath.row];
        NSString *cellValue =theCellData.category;
        NSLog(@"cellValue = %@",cellValue); //To see the value of string cellValue
        cell.textLabel.text = cellValue;

        return cell;

    }

    else {
        ObjectData *theCellData = [arrTwo objectAtIndex:indexPath.row];
        NSString *cellValue =theCellData.category;
        cell.textLabel.text = cellValue;

        return cell;        
    }
}
于 2013-03-01T05:23:51.217 に答える
0

TableViewデリゲートメソッドはすでにセクションを:(NSInteger)section .. として与えているので、switchCaseも使用できます

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        switch (section)
        {
            case 0:
                return self.arrMedia.count;
                break;
            case 1:
                return 1;
                break;
        }
        return 0;
    }
于 2014-12-02T11:57:35.537 に答える