Question

I'm trying to display information into a grouped UITableView with 2 dynamic cells. Each grouped UITableView needs to be called 10 times into a single view; each grouped UITableView displaying different information among its 2 cells.

Right now I'm trying to display all data from my database that's stored in posts. However, when I run this version, the app crashes. If I change return [self.posts count] in numberOfSectionsInTableView: to return 1, I'm able to load one section only, as predicted.

My question is how do I display 10 grouped table sections, each with different info?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return [self.posts count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


NSInteger returnValue = 2;
switch (section) {
    case 0:
    {
        returnValue = 2;
        break;
    }
    default:
        break;
}

return returnValue;

}


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

NSString *CellIdentifier1 = @"Cell";


UITableViewCell *cell;
if (indexPath.section==0)
{
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.row==0)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"];
            }
    if (indexPath.row==1)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"message"];
            }
}

return cell;

}
Was it helpful?

Solution 2

The code which are written is enough for your situation. Just remove this line if (indexPath.section==0){ in cellForRowAtIndexPath.

Update: According to your point , following code is enough. If you correctly stored into posts

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

NSString *CellIdentifier1 = @"Cell";


UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.row==0)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"name"];//Here modified `section` instead of `row`
            }
    else if (indexPath.row==1)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"message"];
            }


return cell;

}

OTHER TIPS

you are creating the cell when section = 0. you should create the cell each time. You can not return nil from cellForRowAtIndexPath method.

Modify your implementation in below way:

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

    NSString *CellIdentifier1 = @"Cell";        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    if (cell == nil)
    {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.section==0)
    {
        if (indexPath.row==0)
        {
            cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"];
        }
        //Rest of the code..
    }
    else if(indexPath.section ==1)
    {
        //Do the work with section 1.
    }
    //do the work for other sections...

    return cell;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top