0

I am making tableview with different sections programmatically. I am using this code Please help me where i am wrong. I have to show different sections with different cells ,sections of years and cells of movies.

in .h file

{
 NSDictionary *movieTitles;
    NSArray *years;
}
@property (nonatomic, retain) NSDictionary *movieTitles;
@property (nonatomic, retain) NSArray *years;

in .m file

@synthesize movieTitles;
@synthesize years;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"about" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];

    movieTitles = dic;


    aboutTable = [[UITableView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height) style:UITableViewStyleGrouped];
    aboutTable.delegate = self;
    aboutTable.dataSource = self;

    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
    [backButton addTarget:self action:@selector(backdb) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [self.view addSubview:backButton];

    [self.view addSubview:aboutTable];


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

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *details = [years objectAtIndex:section];
    NSArray *titleSection = [movieTitles objectForKey:details];

    return [titleSection count];

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSString *details = [years objectAtIndex:[indexPath section]];
    NSArray *titleSection = [movieTitles objectForKey:details];
    cell.textLabel.text = [titleSection objectAtIndex:[indexPath row]];


    return cell;

}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *details = [years objectAtIndex:section];
    return details;

}
  • numberOfSecution is 0 because "year array" is nill . make sure to [alloc]init] the data source array – engmahsa Oct 06 '15 at 06:38

5 Answers5

1
  1. You are returning the [years count] for numberOfSectionsInTableView. Where you initialise the years?
  2. And also in viewDidLoad, instead of movieTitles = dic, use the below code:-

    movieTitles = [dic copy];

pkc456
  • 8,350
  • 38
  • 53
  • 109
  • "This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether." – Daij-Djan Oct 06 '15 at 06:35
  • Welcome @AayushKatiyar. I am very glad that your problem resolved. – pkc456 Oct 06 '15 at 06:37
  • I have made a tableview with 4 sections and i want to add edit buttons in each section of the table. how to do this please help me ,i am trying but not get the success . –  Oct 06 '15 at 09:38
  • Implement the table view delegate method. Reference : http://stackoverflow.com/a/15611529 – pkc456 Oct 06 '15 at 09:44
  • -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ //put your values, this is part of my code UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)]; [view setBackgroundColor:[UIColor redColor]]; UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 150, 20)]; [lbl setFont:[UIFont systemFontOfSize:18]]; [lbl setTextColor:[UIColor blueColor]]; [view addSubview:lbl]; [lbl setText:[NSString stringWithFormat:@"Section: %ld",(long)section]]; return view; } –  Oct 06 '15 at 09:54
  • i am using this but this view hides the heading of each section and how to set the height of each view as the height of section? –  Oct 06 '15 at 09:57
  • There is a delegate method of setting the height of header in sections, implement that as well. – pkc456 Oct 06 '15 at 10:20
0

where is your "year" value. It count become Zero. and you have to alloc your value.so first of all add object to array.

e.g.years =[[NSArray alloc]initWithObjects:@"2001",@"2002",@"2003", nil];

yankit Patel
  • 331
  • 1
  • 12
0

Table view don't show beacuse "years is nil" when called

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

as the number of section return 0, so the tableveiw could not add.

resolve this issue.

add this line in viewDidLoad method

years  = @[@"2015",@"2015"];
Jamil
  • 2,977
  • 1
  • 13
  • 23
0
- (void)viewDidLoad {

NSArray *arrYear;
arrYear=[[NSArray alloc]initWithObjects:@"1991",
@"1992",
@"1993",
@"1994",nil]

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
          NSString *cellIdetifier = [NSString stringWithFormat:@"Cell_%@",indexPath];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier ];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];

      cell.textLabel.text = [arrYear objectAtIndex:[indexPath row]];
    }

    return cell;

}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   return years.count;
}
Chirag
  • 29
  • 2
0
arrYear=[[NSArray alloc]initWithObjects:@"1991",
@"abc",
@"xyz",
@"123",nil]

You have to initialise Your Array values in didload

Hitesh Boricha
  • 114
  • 1
  • 11