PDA

View Full Version : Table doesn't Scroll Programmatically




Sergio10
Nov 10, 2009, 11:52 AM
Hi,

I need on button clicking to scroll table programmatically. But the following code does nothing:

- (IBAction)clickAction:(id)sender
{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case ZERO_VALUE:
{
return firstCell;
} break;

case 1:
{
return secondCell;
} break;

case 2:
{
return thirdCell;
} break;
};
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellAccessoryNone;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


Guys, how to solve the problem?



dejo
Nov 10, 2009, 11:58 AM
- (IBAction)clickAction:(id)sender
{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[tableView reloadData];
}


Guys, how to solve the problem?
Where are you setting the indexPath that's used in your IBAction?

Sergio10
Nov 11, 2009, 08:57 AM
- (IBAction)clickAction:(id)sender
{
NSIndexPath *indexPath = [[NSIndexPath alloc] init];
indexPath = [NSIndexPath indexPathForRow:1 inSection:0];

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[tableView reloadData];
}

It is also don't work.

Troglodyte
Nov 11, 2009, 09:09 AM
- (IBAction)clickActionid)sender
{
NSIndexPath *indexPath = [[NSIndexPath alloc] init];
indexPath = [NSIndexPath indexPathForRow:1 inSection:0];

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[tableView reloadData];
}

As an aside, this will leak memory. You don't need to alloc-init the indexPath.

dejo
Nov 11, 2009, 11:52 AM
It is also don't work.
tableView is an instance variable that's defined and assigned elsewhere?

P.S. And if you really want to scroll to the very top, you need to use:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
Rows (and sections) are zero-indexed.