Arthur Camara
Arthur Camara

Reputation: 577

UITableView scrolls bounces back to top

I've been to a lot different questions on StackOverflow, But I just can't figure what is wrong here. I have a view controller that receives data from a JSON, creating an array, and, then, it builds an UITableView, with fixed heights. The issue is that I can't scroll to the bottom. It just bounces back.

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

  [cell.detailTextLabel setNumberOfLines:2];

  NSDictionary* place = [_placesData objectAtIndex:indexPath.row];
  [cell.textLabel setText:[place valueForKey:@"nome"]];
  [cell.detailTextLabel setText:[place valueForKey:@"endereco"]];
  [cell.detailTextLabel setLineBreakMode:NSLineBreakByWordWrapping];
  [cell.detailTextLabel sizeToFit];
  UIImage* originalImage = [UIImage imageNamed:@"encontre.png"];
  UIImage* resized = [UIImage imageWithCGImage:[originalImage CGImage]scale:(originalImage.scale * 1.8) orientation:(originalImage.imageOrientation)];
  cell.imageView.image=resized;
  cell.textLabel.font = [UIFont fontWithName:@"GillSans" size:17];
  cell.textLabel.textColor = [UIColor whiteColor];
  cell.detailTextLabel.font = [UIFont fontWithName:@"GillSans-Light" size:17];

  self.tableView.scrollEnabled=YES;
  self.tableView.bounces=YES;
  [self.tableView setAlwaysBounceVertical:YES];
  return cell;
}

I have no idea on what else to do. Already tried to set the contentSize.height manually, force bounces and scrollEnabled on almost evert piece of code on the view controller.

Regards.

Upvotes: 2

Views: 4793

Answers (8)

Eli017
Eli017

Reputation: 91

Keep in mind, my solution uses constraints. I ran across this issue while making a UITableView that can have a dynamic number of cells expand with more details. Here was my structure:

  • UIView
    • UIStackView
      • UIView (My Tabs Segue View)
      • UIView (My First Tab View)
        • UITableView (My Table I Wanted to be Scrolled)
        • UIView (My View if that Table was Empty)
      • UIView (My Second Tab View)
        • UITableView (My Second Table I Wanted to be Scrolled)
        • UIView (My View if that Table was Empty)

So, what I found out was that when I was setting the height constraint of the tables to the contentSize of the tables themselves. This originally helped account for the expandable part of the cell. But, if you want a table to be scrollable, you need to have its height shorter than its content height. By making it shorter (and having all the xib checkboxes checked as mentioned in other posts), it will automatically scroll. Granted, you can set its height via constraint or any way you want, just make sure its not the same as its content height!

Upvotes: 0

Arthur Camara
Arthur Camara

Reputation: 577

Just managed to solve it, if anyone is having this same issue.

What I did is, inside the size inspector for my UiTableView, I manually set "Row Height" at 70 (the exact size I'm using).

After this, everything worked as a charm. But, if anyone can give a comprehensive explanation on what is really happening in here, it would be really great.

Upvotes: 1

er.vish
er.vish

Reputation: 267

Actually your problem is related to frame of the table. by setting "Row height" is working for you because by chance count of row in your table and row height giving a table height that is suitable to you. But its not the right way of doing this. Somewhere you need to check height of the table may be something like

Blockquote (nameArray.count<10?kSACellHeight*nameArray.count:kSACellHeight*11))

Upvotes: 1

Ted Guo
Ted Guo

Reputation: 67

I've met with same issue.In my situation,I drag a tableview to a custom view controller,which is presented by a push segue.If the amount of data showed in tableview exceeds some number,then I can't touch the cell on bottom of the tableview.

Many ways have been tried:set the frame/content size of the table view,and none works for me.Finally,I find the root cause and solve it in a simple way(although not gracefully).

First,the root cause:the table view created by IB has a width larger then the its parent view controller.Thus,any cell out of view's bound will not be touched.

So,the solution is simple:Go to StoryBoard,adjust the width of table view,making it smaller than the width of parent view.It seems that if one table view is created by StoryBoard,you can't change its frame by code.That's what I've found up to now. I guess it's a bug of StoryBoard.

Help it be useful for other guys.

Upvotes: 0

Murat Zazi
Murat Zazi

Reputation: 357

Try implementing the UITableViewDelegate method tableView: heightForRowAtIndexPath:, don't let it get assigned automatically (i.e. sizeToFit). UITableView can be very unpredictable if you are not very specific and if you don't override certain methods.
I had a unique problem with tableView scrolling back up to top automatically after I called [tableView reloadData]; This problem was unique because it only happened on iPad mini and iOS 8, every other device and OS was working properly. Hope it helps someone...

Upvotes: 1

Loic
Loic

Reputation: 11

I just had the same problem.

My solution was to update the contentSize before reloading the table's data.

tableViewOffre.contentSize = CGSizeMake(tableViewOffre.frame.size.width, [app.offres count] * 105);

Where 105 is my row height.

I think it isn't the best way to solve the problem (pretty dirty way I guess) but it's the only solution found.

Upvotes: 1

JackPardshe
JackPardshe

Reputation: 21

The way UITableView works is that it requires to know each row height in order to be able to compute its size.

The default behavior is to assume each row height is 44px. Which was clearly not your case here as you said it was 70px. That's why you had to change it in order to be able to scroll all way down

For instance let's say you had 10 rows. With default row height your table view was only able to scroll down to 10*44 = 440px thus the bouncing effect you got. By setting the row height to 70px your tableview now goes down to 10*70 = 700px

Upvotes: 2

chawki
chawki

Reputation: 887

can you check in the xib of your ViewController, select your tableView and click "size inspecto" in the right menu and you change in "iOS 6/7 Deltas": you can tape -20 in height

i think the problem is in adaptation with ios7 and 6

Upvotes: 1

Related Questions