Sectioned TableView shows only one row

Discussion in 'iOS Programming' started by tutiplain, Mar 24, 2011.

  1. tutiplain macrumors member

    Joined:
    Feb 4, 2011
    #1
    Hi everyone,

    I was able to set up my simple table view with one section, Now I am trying to set up a multiple section TableView, but after setting everything up as suggested in a tutorial, only the first row of the first section shows up on the screen. While using debugger, I notice that tableView:cellForRowAtIndexPath: gets called only once, and I haven't the slightest idea why.

    Here is the code for my tableView:cellForRowAtIndexPath method

    Code:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    	static NSString *MyIdentifier = @"MyIdentifier";
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if (cell == nil) 
    	{ 
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
    	}
    	
    	NSDictionary *dict = [listaDeItems objectAtIndex:indexPath.section];
    	NSMutableArray *ar = [dict objectForKey:@"Items"];
    	Descuento *desc = [ar objectAtIndex:indexPath.row];
    	cell.textLabel.text = [desc getAsFinalCalculatedValue:[salario_bruto floatValue]];
    	cell.detailTextLabel.text = [desc getDescuentoText];
    
    	return cell;
    }
    
    As explanation:
    "listaDeItems" is a NSMutable array. It contains to NSDictionary objects with a single key: @"Items". Each @"Items" key points to another NSMutableArray, with one or more objects of type "Descuento" inside. the methods "getAsFinalCalculatedValue and getDescuentoText both return NSString objects. While debugging, I've made sure that all objects are loaded properly, so my error shouldn't be the data model (I hope).

    My table data source and delegate is a custom class (I manually implemented both UITableViewDataSource and UITableViewDelegate protocols). Any info on this will be very much appreciated. Thanks in advance.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What do your numberOfSectionsInTableView: and tableView:numberOfRowsInSection: datasource methods look like?
     
  3. tutiplain thread starter macrumors member

    Joined:
    Feb 4, 2011
    #3
    Hi again,

    Here is the code for the methods you mentioned:

    Code:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    	return [[listaDeItems objectAtIndex:section] count];
    }
    
    -(NSInteger) numberOfSectionsInTableView
    {
    	return 2; //since I intend to use only two section in the tableView
    }
    
    
    Thanks for any advice you can offer!
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    And you're certain that tableView:numberOfRowsInSection: is returning the correct values for each section?

    Also, is the frame of your tableView large enough to show all the rows?
     
  5. tutiplain thread starter macrumors member

    Joined:
    Feb 4, 2011
    #5
    Hello again.

    Yes, my table view is big enough for up to 8 rows of data (my programming should only show 3, though). While debugging to answer your other question,
    I found that the members of the "listaDeItems" mutable array were "out of scope", and therefore the method was returning incorrect values. So I changed my method like this (and the first section of the data showed up completely:

    Code:
     
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    	NSDictionary *dict = [listaDeItems objectAtIndex:section];
    	NSMutableArray *mut = [dict objectForKey:@"Items"];
    	NSUInteger x=  mut.count;
    	//NSUInteger x = [[listaDeItems objectAtIndex:section] count];
    	return x;
    }
    
    Why this longer approach to do exactly the same thing is beyond me, but it does, at least for the first section, the second one (with only one row) is not yet visible in my table view. I also wonder if this causes memory leaks, since I am not [release]ing the variables once done with them. This is almost solved, thanks to your insight, now I just need to find out how to display the second section. Any ideas?
     
  6. tutiplain thread starter macrumors member

    Joined:
    Feb 4, 2011
    #6
    Update: Debugging shows me that numberOfRowsInSection: is getting called only once. Isn't it supposed to be called once for each section?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It is.

    I suspect that your custom class to handle your datasource and delegate is not working as hoped. You say you are following a tutorial. It would really help if you told us which one (including a link to it, if possible).
     
  8. tutiplain thread starter macrumors member

    Joined:
    Feb 4, 2011
    #8
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    If we could see the code for your entire table view controller class as well as any code / IB screenshots that displays the table view, that would probably help.
     
  10. tutiplain thread starter macrumors member

    Joined:
    Feb 4, 2011
    #10
    Hi again,

    I was able to solve the problem. It seems my definition of the numberOfSectionsInTableView: was not defined properly (it didn't have "(UITableView*) tableView " at the end, so it wasn't getting called. I found it by placing breakpoints in cellForRowAtIndexPath and numberOfRowsInSection:

    Thanks again for all your help.
     

Share This Page