murali
murali

Reputation: 11

How to make uitableview with multiple sections dynamically?

How to make uitableview with multiple sections dynamically with dynamic array count ?

Upvotes: 0

Views: 4879

Answers (4)

NANNAV
NANNAV

Reputation: 4901

You set totalSection and reload tableview

Before you set totalSection in [tableView reloadData];

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

Upvotes: 0

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Please try to use this one....

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   // ----- here you can return any value whatever you want. -----
    return [array count];
}

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

Check this long code, I think this will come handy

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark TableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.words count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.words objectAtIndex:section]count];
}

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

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

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

    cell.textLabel.text=[[self.words objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"This is detailed subtitle for %@.",cell.textLabel.text];
    //cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

    NSString *imagePath=[[NSBundle mainBundle]pathForResource:@"images" ofType:@"jpeg"];
    UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
    cell.imageView.image=image;


    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.alphabets objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;{
    return self.alphabets;
}

#pragma mark -
#pragma mark Default



- (void)viewDidLoad
{

    self.alphabets=[NSMutableArray new];
    for (char i=97; i<123; i++) {
        [self.alphabets addObject:[[NSString stringWithFormat:@"%c",i]capitalizedString]];
    }

    self.words=[NSMutableArray new];
    for (NSString *character in _alphabets) {
        NSMutableArray *twoD=[NSMutableArray new];
        for (int i=1; i<(rand()%10)+1; i++) { //fill randamly any number 1 to 10.
            [twoD addObject:[NSString stringWithFormat:@"%@%d",character,i]];
        }
        [self.words addObject:twoD];
    }

    // NSLog(@"->%@",self.words);


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 4

Stuart M
Stuart M

Reputation: 11588

You probably should look at the UITableViewDataSource API documentation, specifically the -[UITableViewDataSource numberOfSectionsInTableView:] API.

You will also need to implement -[UITableViewDataSource tableView:numberOfRowsInSection:]. Both of these would need to be implemented in your class which implements the UITableViewDataSource protocol.

Upvotes: 0

Related Questions