Background: I'm working on my first iOS app that uses a UITableView for data being loaded dynamically from a web source. No, this is not homework.
Problem: When I scroll down the list bounces back to the top.
Things I've Tried:
- This suggestion (reducing the size of the table view) works but now I have large space at the bottom of the screen.
- I've tried using
[tableView reloadData]
but this doesn't changing the behavior. I even hooked this up to a button to make sure it was firing after the view was populated. - I also read a few post about
contentSize
and how to calculate based on the amount of data loaded in the table. This looks like the solution I need but am having trouble getting a clear explanation of how to implement it for my setup.
Code:
I started a new project with single view application template and added a UITableView
to the default view in the main storyboard.
In view controller I have the following (relevant) code:
@interface ERViewController ()
@property (nonatomic, strong) NSMutableArray *myArray;
@property (nonatomic,retain) UITableView *tableView;
@end
@implementation ERViewController
{
NSMutableArray *tableData;
}
Here I am loading the data from Parse.com into my table and then calling [self loadView]
to get the view to update after the query finishes.
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *returnedData = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:@"Persons"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
//NSLog(@"display name: %@",object[@"firstName"]);
[returnedData addObject:[NSString stringWithFormat:@"%@, %@", object[@"lastName"],object[@"firstName"]]];
}
tableData = returnedData;
// Reload view to display data
[self loadView];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
NSArray *itemArray = [NSArray arrayWithObjects: @"✔︎", @"?", @"X", nil];
UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:itemArray];
mainSegment.frame = CGRectMake(200,0, 100, 43);
self.navigationItem.titleView = mainSegment;
mainSegment.selectedSegmentIndex = [itemArray count];
[mainSegment addTarget:self
action:@selector(mainSegmentControl:)
forControlEvents: UIControlEventValueChanged];
mainSegment.tag = indexPath.row;
[cell.contentView addSubview:mainSegment];
return cell;
}
Sample screen shot:
Since I'm waiting for the data to load I'm not sure where to resize the view and how exactly I need to resize the view.