0

Uitableview problem again. Whenever I reload my data, the tableview would reload but it seems like the data will not start at the first row. Please refer to the image below.

I tried the following after the reload of data but still no success:

[self.tableView reloadData];
// this or the other one ... [self.tableView setContentOffset:CGPointZero animated:NO];
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

This is how I positioned the uitableview on viewdidload

self.tableView.frame = CGRectMake(450, 20, self.tableView.frame.size.width, self.tableView.frame.size.height - 20);

EDIT 1:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SearchResultCell";

    User *user = [users objectAtIndex:indexPath.row];
    UserTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UserTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.name.text =  [NSString stringWithFormat:@"%@ %@", user.firstName, user.lastName];

    return cell;
}
gdubs
  • 2,724
  • 9
  • 55
  • 102

1 Answers1

1

Based on your comments, your table is being resized incorrectly when you are rotating the device to landscape mode. In your storyboard, you need to set the autoresizing on the table view in order for it to properly resize the way that you want.

Here is a good starting point for something like this:

enter image description here

Notice just above the Autosizing label, it shows a bunch of red bars (called springs and struts). I have them all turned on, which will maintain the distance from the edge of the superview and allow the object to change its' size in order to keep those distances the same. This should be close to what you want, and if not then you can play around with the different combinations until you get what you want.

You turn each part of it on and off by clicking on the red line.

For more details, take a look at this question: Autoresizing masks programmatically vs Interface Builder / xib / nib

Community
  • 1
  • 1
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • will this be after reloading? or even at the viewdidload? – gdubs May 02 '13 at 02:57
  • Yes, after reloading instead of your call to scrollRectToVisible. – lnafziger May 02 '13 at 02:58
  • hmm.. tried it and it's still giving that effect. Also, it might be of help to know that I took out the autolayout. – gdubs May 02 '13 at 02:59
  • Try adding NSLog("%@", self.tableView); and see what the frame is after reloading the data. – lnafziger May 02 '13 at 03:08
  • Oops my bad, this is what it shows >> ; layer = ; contentOffset: {0, 0}> – gdubs May 02 '13 at 03:54
  • 1
    Okay, notice that your frame has been changed to a y position of -57. This is why you can't see the top of your table. I would guess that it is because of your autoresize mask (your log shows TM+BM). I bet if you change the orientation of the simulator/device to portrait, it will look fine. – lnafziger May 02 '13 at 03:57
  • oh damn you're right!!!! wait, so it gets messed up on landscape and is perfect on portrait. How do I maintain it on landscape? – gdubs May 02 '13 at 04:08
  • You need to set your autosizing rules so that the table resizes when the view does (ie when the device is rotated). – lnafziger May 02 '13 at 04:32
  • Hm.. Not really sure as to how to do this. Any chance you can point me to a sample code I can follow? – gdubs May 02 '13 at 18:34
  • Did you make the table in code, or in a storyboard/xib? If code, post all of the code used to create it. Otherwise, you set the constraints in the storyboard on the Size Inspector. – lnafziger May 02 '13 at 21:53
  • Sorry for the late reply. I'm using storyboards. I'll get you the results after i get off work. – gdubs May 07 '13 at 13:06
  • @Inafziger , so I'm still trying to figure out how to set the constraints on the Size Inspector, I have no clue as to how to configure when the unit goes from portrait to landscape. – gdubs May 09 '13 at 03:56
  • should I be doing this on the prototype cell or on the uitableview? – gdubs May 10 '13 at 04:40
  • hmm.. still doesn't seem to work. It's just the autosizing that I need to be worried about right? – gdubs May 12 '13 at 23:24
  • Yeah, that's all unless you are changing the size/position of the tableview in code. – lnafziger May 12 '13 at 23:32