Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,

I need on button clicking to scroll table programmatically. But the following code does nothing:
PHP:
- (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?
 
Code:
- (IBAction)clickAction:(id)sender
{
    [tableView scrollToRowAtIndexPath:[COLOR="Red"][B]indexPath[/B][/COLOR] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [tableView reloadData];
}

Guys, how to solve the problem?
Where are you setting the indexPath that's used in your IBAction?
 
Code:
- (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.
 
Code:
- (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.
 
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:
Code:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[B][COLOR="Red"]0[/COLOR][/B] inSection:0];
Rows (and sections) are zero-indexed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.