Question

I want a sectioned UITableView but I don't know how, my situation:

I have 2 arrays

array Date ("12/08/13", "13/08/13", "17/08/13") array Count("2", "1", "5")

The count means how many rows are there in the section.

I have

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

But how can make in the first section, 2 rows in the second section 1 row and in the third section 5 rows.

I hope someone can help me. If you have questions just ask.

Thanks.

EDIT: I get my information from my website so the array changes everyday.

Was it helpful?

Solution

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // length of your section count array:
    return [sectionCountArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section            
{
    // the section count from your array, converted to an integer:
    return [sectionCountArray[section] integerValue];
}

UPDATE: From your comment is seems that you have a "flat" data source array. Since the row numbers start with zero in each section, you have to compute the index into the array from the row number and the section count of all previous sections in cellForRowAtIndexPath:

NSInteger flatIndex = indexPath.row;
for (NSInteger sec = 0; sec < indexPath.section; sec++)
    flatIndex += [tableView numberOfRowsInSection:sec];

cell.textLabel.text = yourFlatDataArray[flatIndex]; // (Just an example!)

OTHER TIPS

You return the number of sections in the numberOfSectionsInTableView:

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

Then get the number from the array and return that in the numberOfRowsInSection:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section            
 {
      NSNumber *count = [self.countArray objectAtIndex:section];

      return [count integerValue];
 }

You need to override the following UITableView datasource method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (section == 0)
    return 2
  else if (section == 1)
    return 1
  else (section == 2)
    return 5
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top