I am trying to add section titles to my UITableView. I am able to create the sections and count the number of elements in each section properly, but when I call my cellForRowAtIndexPath method, the table repeats the data in both sections.
Here is where I prep the UITableView Section Stuff:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int sections = 0;
if (sectionOneCount>0) {
sections++;
}
if (sectionTwoCount>0) {
sections++;
}
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
return sectionOneCount;
}
else {
return sectionTwoCount;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0) {
return @"Section One Title";
}
else {
return @"Section Two Title";
}
}
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MainCell";
ProfileTableViewCell *cell = [mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.label1.text = [[items objectAtIndex:indexPath.row] valueForKey:@"label1"];
cell.label2.text = [[items objectAtIndex:indexPath.row] valueForKey:@"label2"];
return cell;
}
Any ideas on what I'm doing wrong here? Thanks!
P.S. Here is some sample data that I am working with. I only have two possible sections, one for each type (in this case, red and blue). I have one main array called items (as you can see in my cellForRowAtIndexPath methods).
{
"parentArray": {
"id": 5,
"items": [
{
"type": "red",
"title": "RedItem1"
},
{
"type": "red",
"title": "RedItem2"
},
{
"type": "blue",
"title": "BlueItem1"
},
{
"type": "blue",
"title": "BlueItem2"
},
{
"type": "blue",
"title": "BlueItem3"
}
]
}
}