Table View methods not called

Discussion in 'iOS Programming' started by Spencie, Mar 30, 2013.

  1. Spencie macrumors regular

    Spencie

    Joined:
    Aug 17, 2011
    Location:
    The Mothership
    #1
    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];
        
    }
    
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Have you confirmed this? Have you done any other debugging?

    Also, you don't need to call [tableView reloadData]; inside your tableView:cellForRowAtIndexPath:
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    In fact, doing so will likely cause an infinitely loop, possibly an infinite recursion crash.
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    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.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Never call reload from cellForRowAtIndexPath. Ever, under any circumstances.

    It isn't clear what you're saying. Post your code.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    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.
     
  7. Spencie thread starter macrumors regular

    Spencie

    Joined:
    Aug 17, 2011
    Location:
    The Mothership
    #7
    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.
     
  8. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #8
    Re your signature

    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.)
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     

Share This Page