Stanley
Stanley

Reputation: 404

Divide UITableView into sections with Objective-C in Xcode11

I'm new to iOS development in Xcode 11 with Objective-C, so please bear with me.

My goal is to create two pages, where in the first page have a single button, which after pressing brings to the second page. In the second page I have a simple list with some data.

Im trying to divide this TableView into sections. So far, I have created a button and can "jump" from one page to another with Segue. But got trouble with filing the list with data. I'm trying to make exactly this page:
Correct Output

Bu instead I'm getting the list without any data:
Error output

This is my main.storyboard:
Page structure

For the second view, I created a new class SecondControllerView.m and attached this class to the second page
attaching class to the View

  //SecondControllerView.m  
#import "SecondViewController.h"

@interface SecondViewController (){
NSMutableArray *numArray;
NSMutableArray *numArray1;
NSMutableArray *numArray2;
NSMutableArray *numArray3;
NSMutableArray *numArray4;
NSMutableArray *numArray5;
NSMutableArray *numArray6;
NSMutableArray *numArray7;

NSMutableArray * sectionArray;
}
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    numArray = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray1 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray2 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray3 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray4 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray5 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray6 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
    numArray7 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];

    sectionArray = [NSMutableArray arrayWithObjects:@"tour 1",@"tour 2",@"tour 3",@"tour 4",@"tour 5",@"tour 6",@"tour 7",@"tour 8",nil];
    NSLog(@"Hello world");
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return sectionArray.count;
}

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

-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    int count = 0;
    if(section == 0){
        return numArray.count;
    }else if (section == 1){
        return numArray1.count;
    }else if (section == 2){
        return numArray2.count;
    }else if(section == 3){
        return numArray3.count;
    }else if(section == 4){
        return numArray4.count;
    }else if(section == 5){
        return numArray5.count;
    }else if(section == 6){
        return numArray6.count;
    }else if(section == 7){
        return numArray7.count;
    }
    return count;
}

-(nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if(indexPath.section == 0){
        cell.textLabel.text = numArray[indexPath.row];
    }else if(indexPath.section == 1){
        cell.textLabel.text = numArray1[indexPath.row];
    }else if(indexPath.section == 2){
        cell.textLabel.text = numArray2[indexPath.row];
    }else if(indexPath.section == 3){
        cell.textLabel.text = numArray3[indexPath.row];
    }else if(indexPath.section == 4){
        cell.textLabel.text = numArray4[indexPath.row];
    }else if(indexPath.section == 5){
        cell.textLabel.text = numArray5[indexPath.row];
    }else if(indexPath.section == 6){
        cell.textLabel.text = numArray6[indexPath.row];
    }else if(indexPath.section == 7){
        cell.textLabel.text = numArray7[indexPath.row];
    }
    return cell;
}

@end   

What could be wrong here ?

Upvotes: 0

Views: 675

Answers (1)

AntiVIRUZ
AntiVIRUZ

Reputation: 342

First, make SecondViewController the Delegate and DataSource of the table view https://guides.codepath.com/ios/Table-View-Guide

Second, do [self.tableView reloadData] in the end of viewDidLoad

Upvotes: 2

Related Questions