Search code examples
iosobjective-cuitableviewnsarraynsdictionary

How to display two NSArray objects as sections of UITableview using NSDictionary in ios


Here is my code ViewController.m

{
    NSArray *sectionTitleArray;

}
@property (strong, nonatomic) IBOutlet UITableView *tablevw;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tablevw.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    _tablevw.delegate = self;
    _tablevw.dataSource = self;

    sectionTitleArray = @[ @{ @"description": @"About Step0ne",
                              @"About Stepone": @[ @{ @"text": @"Step0ne 1" },
                                              @{ @"text": @"Step0ne 2" },
                                              @{ @"text": @"Step0ne 3" },
                                              @{ @"text": @"Step0ne 4" },
                                              @{ @"text": @"Step0ne 5" },
                                              @{ @"text": @"Step0ne 6" },
                                              @{ @"text": @"Step0ne 7" },
                                              @{ @"text": @"Step0ne 8" },
                                              ]
                              },
                           @{ @"description": @"Profile",
                              @"profile": @[ @{ @"text": @"profile 1" },
                                              @{ @"text": @"profile 2" },
                                              @{ @"text": @"profile 3" },
                                              @{ @"text": @"profile 4" },
                                              @{ @"text": @"profile 5" },
                                              @{ @"text": @"profile 6" },
                                              @{ @"text": @"profile 7" },
                                              @{ @"text": @"profile 8" },
                                              ]
                              },

                              ];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:@"About Stepone"];
    cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:@"Profile"];

    return cell;
}

About Stepone and Profile are the two sections of my tableview. Whereas stepone1,stepone2,....stepone8 should be my rows of About Stepone section .


Solution

  • Here is some tried and tested code for you. I have made some changes in your data structure for the purpose of ease. Hope you find that helpful.

    {
        NSDictionary *_objects;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        _objects = @{@"About Stepone": @[ @{ @"text": @"Step0ne 1" },
                                          @{ @"text": @"Step0ne 2" },
                                          @{ @"text": @"Step0ne 3" },
                                          @{ @"text": @"Step0ne 4" },
                                          @{ @"text": @"Step0ne 5" },
                                          @{ @"text": @"Step0ne 6" },
                                          @{ @"text": @"Step0ne 7" },
                                          @{ @"text": @"Step0ne 8" }],
                     @"profile": @[ @{ @"text": @"profile 1" },
                                    @{ @"text": @"profile 2" },
                                    @{ @"text": @"profile 3" },
                                    @{ @"text": @"profile 4" },
                                    @{ @"text": @"profile 5" },
                                    @{ @"text": @"profile 6" },
                                    @{ @"text": @"profile 7" },
                                    @{ @"text": @"profile 8" }]
                     };
    
    }
    
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table View
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return _objects.allKeys.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return ((NSArray *) _objects[_objects.allKeys[section]]).count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        NSDictionary *object = _objects[_objects.allKeys[indexPath.section]][indexPath.row];
        cell.textLabel.text = object[@"text"];
        return cell;
    }
    
    - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return _objects.allKeys[section];
    }
    

    Happy Coding.

    Output is like:

    Output of above lines