UITableView With Multiple Sections

70,725

Solution 1

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

Solution 2

Here are the relevant Swift 3 lines:

func numberOfSections (in tableView: UITableView) -> Int {
    ...
    return numberOfSections
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    //Not necessary, but will let you format the section headers. Example:
    guard let header = view as? UITableViewHeaderFooterView else { return }
    header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
    header.textLabel?.textColor = UIColor.darkGray
    header.textLabel?.textAlignment = .center
    header.textLabel?.frame = header.frame
    view.tintColor = UIColor.white
}

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    //Also not necessary, but clear footers help space out sections
    view.tintColor = UIColor.clear   
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    //Spacing between sections:
    return 25
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    ...
    return sectionCount
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //All your cell setup
    ...
    //I call this to get the absolute row number (disregarding sections)
    let row = getAbsoluteRow_InCurrentSection(indexPath: indexPath)
    //Now you can use the row number to grab a value from an array or CoreData object and use the value to populate your cell!! 
}

func getAbsoluteRow_InCurrentSection(indexPath: IndexPath) -> Int {
    var aggRows = 0
    if currentListEntity == "GrocTask" {
        let curSection = indexPath.section
        for i in 0..<curSection {
            aggRows += flex1ArrayCnt[i]
        }
        aggRows += indexPath.row
    }
    return aggRows
}

//Please be aware, I only included those directly relevant to building a UITableView with sections. I did not include editActions, didSelect, or other UITableView delegate functions.

Share:
70,725
Nazia Jan
Author by

Nazia Jan

Updated on July 09, 2022

Comments

  • Nazia Jan
    Nazia Jan almost 2 years

    I want to make tableView with multiple section but i do not want to use dictionary i have two arrays i want that first array should be loaded in first section and second in second sections.

    I have arrayOne with 3 items and arrayTwo with 4 items so how to add them and show them in sections.

    - (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;       
         }
    }
    
  • Nazia Jan
    Nazia Jan about 11 years
    again it is giving error i have done like this indexPath.section
  • Baby Groot
    Baby Groot about 11 years
    i guess P should be capital in indexpath,.... have edited that. Now try, it should work.
  • Rushi
    Rushi about 11 years
    This - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (indexPath.section==0) { return [array1 count]; } else{ return [array1 count]; } } is wrong. You can't get indexPath.section here.
  • Shashank Kulshrestha
    Shashank Kulshrestha about 11 years
    Can you NSLog you cellValue and see if there is some data in it or not
  • Nazia Jan
    Nazia Jan about 11 years
    it is not showing NSLog of anything in where we wrute the method for cellForRowAtIndex except that in sections and number of rows NSLog is working so it means it is not entering in rowForIndexPath
  • Rushi
    Rushi about 11 years
    @NaziaJan did u implement this function?
  • Baby Groot
    Baby Groot about 11 years
    i have added one line of code, add that in your code and see if you are getting correct value in your string to print in your cell label. Try that once.
  • Shashank Kulshrestha
    Shashank Kulshrestha about 11 years
  • Cybernetic
    Cybernetic over 10 years
    Good answer...you just need to change the else statement to return the count of array2 not array1.
  • Krutarth Patel
    Krutarth Patel over 7 years
    @ShashankKulshrestha my current section highlight till second section appear.is this by default ?
  • Abdul Saleem
    Abdul Saleem about 6 years
    Awesome.. Thanks.