Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Spencie

macrumors regular
Original poster
Aug 17, 2011
235
0
The Mothership
Hi all, please bear with me as I am new to iOS programming. I have a tableview inside one of my Viewcontrollers, but none of my tableview methods are being called. The table view is also properly linked to datasource and delegate.

For example, below I have many methods that change the size and color/contents of the tableview, but when I run the application, nothing happens. The tableview just looks like the default size/color.

Any input would be helpful.

Code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //tableview cell height
    return 60;
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfSectionsInTableView:(NSInteger)section {
    return 1;


}

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

    return [storyList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
     if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        
    }

    
    //---------- CELL BACKGROUND IMAGE -----------------------------
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
    UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
    imageView.image = image;
    cell.backgroundView = imageView;
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
    
    
    cell.textLabel.text = [storyList objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [storyListAuthor objectAtIndex:indexPath.row];
    

    return cell;
    [tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    detail.state = [datasource objectAtIndex:indexPath.row];
    detail.capital = [storyListAuthor objectForKey:detail.state];
    [self.navigationController pushViewController:detail animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
Have you confirmed this? Have you done any other debugging?

Also, you don't need to call [tableView reloadData]; inside your tableView:cellForRowAtIndexPath:

As luck would have it, in the sample provided, the method is being returned before that call happens. ;)

Interestingly I see no data displayed when I place the reload just before the return. That infinite loop must be blocking the display of the data.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
As luck would have it, in the sample provided, the method is being returned before that call happens. ;)

Interestingly I see no data displayed when I place the reload just before the return. That infinite loop must be blocking the display of the data.

Never call reload from cellForRowAtIndexPath. Ever, under any circumstances.

It isn't clear what you're saying. Post your code.
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
Never call reload from cellForRowAtIndexPath. Ever, under any circumstances.

It isn't clear what you're saying. Post your code.

I was playing with some test code to see if I'd get a crash when placing reload into the cellForRowAtIndexPath method. I didn't but perhaps I didn't run it long enough, on the simulator. Well, that was one thing I was looking at.

As for what did happen, the single row I had working was no longer displayed, I was able to scroll the empty table which also appeared to have a double height top row.
 

Spencie

macrumors regular
Original poster
Aug 17, 2011
235
0
The Mothership
Hi all, I didn't realize all the issues you guys pointed out. Turns out
Code:
  return [storyList count];
returned 0, the array had 0 items. I just set it to return 10 and then it worked. Nevertheless, thanks for pointing out the other issues, I will fix them.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Re your signature

Hi all, I didn't realize all the issues you guys pointed out. Turns out
Code:
  return [storyList count];
returned 0, the array had 0 items. I just set it to return 10 and then it worked. Nevertheless, thanks for pointing out the other issues, I will fix them.

Regarding your signature,

Do you know the programmer's cheer?

Shift to the left,
Shift to the right,
Push down,
Pop up,
Byte byte byte!

(More of a C/assembler programmer's cheer, really.)
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Hi all, I didn't realize all the issues you guys pointed out. Turns out
Code:
  return [storyList count];
returned 0, the array had 0 items. I just set it to return 10 and then it worked.

So, you hardcoded it to return 10 even though your array contains no items? And it works? I would think you'd get a runtime error when trying to call [storyList objectAtIndex:] then.
 

yuvraj.kale

macrumors newbie
Sep 3, 2019
1
0
Hi all, please bear with me as I am new to iOS programming. I have a tableview inside one of my Viewcontrollers, but none of my tableview methods are being called. The table view is also properly linked to datasource and delegate.

For example, below I have many methods that change the size and color/contents of the tableview, but when I run the application, nothing happens. The tableview just looks like the default size/color.

Any input would be helpful.

Code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //tableview cell height
    return 60;
   
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfSectionsInTableView:(NSInteger)section {
    return 1;


}

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

    return [storyList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
     if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
       
    }

   
    //---------- CELL BACKGROUND IMAGE -----------------------------
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
    UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
    imageView.image = image;
    cell.backgroundView = imageView;
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
   
   
    cell.textLabel.text = [storyList objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [storyListAuthor objectAtIndex:indexPath.row];
   

    return cell;
    [tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    detail.state = [datasource objectAtIndex:indexPath.row];
    detail.capital = [storyListAuthor objectForKey:detail.state];
    [self.navigationController pushViewController:detail animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}
Have you checked the height of table view before reloading the table. If your table view height is 0 then none of the method get called.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.