开发者

how to add smooth scroll to UITbaleview when we adding new row to table view

开发者 https://www.devze.com 2023-02-27 11:13 出处:网络
i am getting images with messages from .net webserver by giving xml input. that\'s working fine. i am sending request for every 3 sec and if any new messages and images are there i just add those me

i am getting images with messages from .net webserver by giving xml input.

that's working fine.

i am sending request for every 3 sec and if any new messages and images are there i just add those messages and images to the array and reload the table view.

That is also fine,But what i need is when i reload table view when ever there is new messages that will be displayed 开发者_运维技巧on table view by smooth scrolling the existing row.

same as twitter.

can any one please help me.

Thank u in advance.


if you know the indexPath of newly added row , you could use the below function of UITableView to scroll upto new row with animation (smooth effect).

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated


Instead Of completely reloading the table, you should call insertRowsAtIndexPaths:withRowAnimation:

The second option will let you specify how you want it animated

(this will also be much more efficient than reloading all the data every time)

You can do something like the following: (this makes the assumptions that recievedImageFrom server will be called after the data has already been loaded into the data source object. Also, myDataSourceArray is the array where the data is being stored for the table and myTableView is the UITableView.

-(void)recievedImageFromServer
{
    NSUInteger row = [myDataSourceArray count]-1; // This can also be set to 0 if you want
                                                // to insert at the top of the table

    NSIndexPath* newIndex = [NSIndexPath indexPathWithIndex:row];
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndex]
                       withRowAnimation:UITableViewRowAnimationTop];
}

It will then request the cell from its data source.

0

精彩评论

暂无评论...
验证码 换一张
取 消