Creating Grouped UITableview with Different Cell Types

爷,独闯天下 提交于 2019-12-03 08:47:46

Since you have two different sets of data and you need to display both in different sections, you have to split the data source methods into two.

Basically, choose which dataset you want to be first and off you go.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section)return secondArray.count;
    //Essentially, if statements evaluate TRUE and move forward if the inside is 1 or greater (TRUE == 1)
    return firstArray.count;
    //If the first if statement return hits, then the code will never reach this statement which turns this into a lighter if else statement
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section)
    {
        //do stuff with second array and choose cell type x
    }
    else
    {
        //do stuff with first array and choose cell type y
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell with: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(indexPath.section)
    {
        //perform action for second dataset
    }
    else
    {
        //perform action for first dataset
    }
}

For headers, you can use either of these methods and just keep the same type of styling as above:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

You can create multiple custom subclasses of UITableViewCell, and in the tableView:cellForRowAtIndexPath: method for your UITableViewDataSource, you can use if-statements to determine what type of cell to use.

For example, here's a rough outline of what I might do:

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

    //First, determine what type of object we're showing
    if (indexPath.section == 0) {
         //Create and return this cell.
    } else if (indexPath.section == 1) {
         //Create and return this cell.
    }...
}

Here's how you'd implement numberOfRowsInSection:

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

   if (section == 0) {
      return [firstSectionArray count];
   } else if (section == 1) {
      return [secondSectionArray count];
   } ...
}

For didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   if (indexPath.section == 0) {
      ObjectSelected *objectSelected = [firstArray objectAtIndex:indexPath.row];

      //Now you've got the object, so push a view controller:
      DetailViewController *dvc = [[DetailViewController alloc] init];
      dvc.objectSelected = objectSelected;
      [self.navigationController pushViewController:dvc];
   } else if (indexPath.section == 1) {
      //Same thing, just call [secondArray objectAtIndex:indexPath.row] instead!
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!