My UITableView won't scroll down through the end of the data! Why?

22,742

Solution 1

Decrease your tableView's height.

Solution 2

I know it's an old question and this is not an answer to your question, but a suggestion to improve your code so others can benefit.

You could write this:

if (indexPath.row >= 4 && indexPath.row <= 8) { // 4 and 8 inclusive
    cell.indentationLevel = 2;
}
cell.textLabel.text = @"";


Instead of this:

if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}


And if you happen to have different text for each case you could either use:

switch (indexPath.row) {
    case 0:
        cell.textLabel.text = @"";
        break;
        ...
    case 18:
        cell.textLabel.text = @"";
    default:
        break;
}

Or even better to put everything inside an NSArray (database...) and just loop through them by the index.

Solution 3

This helped me:

[self.tableView setContentInset:UIEdgeInsetsMake(0,0,65,0)];

Solution 4

@EmptyStack answer is right.That was a alternate but NOT the Solution.

Even we can achieve the same in storyboard.

Select tableView --> from the storyboard control click on 'add Missing constraints'. That will add the constraint to tableView and View.

That helped me to resolve this issue. A screen_shot attached for reference.solution

Solution 5

The code that you shared looks fine to me. Are you pushing view controller with this tableview on navigation controller stack ? I have seen people do that before. Default height of cell is 44px and height of navigation bar is 44px as well. So, if you push this on navigation controller stack you scroll view snaps back like you have described. If that is the case reduce height of TableView by 44 px ( in Interface builder or Programatically if you drew this programatically) and it should fix it.

Share:
22,742
Stephen J.
Author by

Stephen J.

Updated on July 11, 2022

Comments

  • Stephen J.
    Stephen J. almost 2 years

    Ok I don't know why this isn't working, but I have hooked up a tableView, with 19 items of text I'd like to set to each cell.

    The cells populate just fine, but when I try and scroll, it goes down there and I can see the cells that aren't visible on the initial view, it hangs, and won't scroll down. It just snaps back. REALLY WEIRD!

    What am I doing wrong?

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    {
    // Return the number of rows in the section.
    return 19;
    }
    
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    if(indexPath.row == 0){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 2){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 3){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 4){
        cell.indentationLevel = 2;
        cell.textLabel.text = @"";
    }else if(indexPath.row == 5){
        cell.indentationLevel = 2;
        cell.textLabel.text = @"";
    }else if(indexPath.row == 6){
        cell.indentationLevel = 2;      
        cell.textLabel.text = @"";
    }else if(indexPath.row == 7){
        cell.indentationLevel = 2;      
        cell.textLabel.text = @"";
    }else if(indexPath.row == 8){
        cell.indentationLevel = 2;      
        cell.textLabel.text = @"";
    }else if(indexPath.row == 9){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 10){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 11){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 12){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 13){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 14){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 15){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 16){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 17){
        cell.textLabel.text = @"";
    }else if(indexPath.row == 18){
        cell.textLabel.text = @"";
    }
    
    return cell;
    }
    
  • SleepNot
    SleepNot about 11 years
    Hi, I am having the same problem. I have tried reducing the size of my table view but it still wouldn't scroll to the bottom and just scrolls back up to the first cell/top of the table view.
  • septerr
    septerr over 10 years
    I think one of the following things cause this issue. 1. Height of the table extends beyond the height of the device. 2. Some other UI element is overlapping the bottom of your table. 3. Changes were made to the table data since the last time it was loaded.
  • BDR
    BDR over 9 years
    Keep in mind that "Add Missing Constraints" can add constraints to margins, which aren't supported on older versions than ios8.
  • abarisone
    abarisone almost 9 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?
  • Jeff
    Jeff over 6 years
    After altering my tableview to use a header cell from an xib I was getting the scroll problem. This solved my problem but Im skeptical of the problems it might cause down stream. There must be something else going on...
  • Jan Bergström
    Jan Bergström over 4 years
    In fact I believe this is the true answer. Just of course the self frame is including the navigation bar. And it can be retrieved I guess by UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height ?? 0.0)
  • Jan Bergström
    Jan Bergström over 4 years
    Also it is an issue by rotation, the heights and widths are not automatically set right.
  • Jan Bergström
    Jan Bergström over 4 years
    Use constraints in cases like this looks to be the only really working way in iOS. Using constraints the lists scrolls to the end.