简体   繁体   中英

how to add new row to UItableview with smooth scrolling

i am adding some images and messages getting from .net web server.

i am refreshing the data for every 3 sec.

After 3 sec i am getting new messages.I need to add these messages and images at the top of the UITableview.

like messages adding in twitter, google realtime .

for that some where i found insertRowsAtIndexPaths:withRowAnimation: rather then reloading the table view.

but i did n't understand how can i use this insertRowsAtIndexPaths:withRowAnimation: method.

if it is correct can any one please post some code other wise please suggest better way for this.

thank u in advance.

You simply create an array with the index paths of the new rows you want to insert ( +[NSIndexPath indexPathForRow:inSection:] ) and then pass that array to insertRowsAtIndexPaths:withRowAnimation: .

The table view will then call the appropriate data source methods ( tableView:cellForRowAtIndexPath: etc.) to ask your data source for the data of the new rows. So the moment you call insertRowsAtIndexPaths:... , your data source must already contain the data for the new rows. And that's it.

insertRowsAtIndexPaths:withRowAnimation: Inserts rows in the receiver at the locations identified by an array of index paths, with an option to animate the insertion.

  • (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation Parameters indexPaths An array of NSIndexPath objects each representing a row index and section index that together identify a row in the table view. animation A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See “Table Cell Insertion and Deletion Animation” for descriptions of the constants. Discussion UITableView calls the relevant delegate and data source methods immediately afterwards to get the cells and other content for visible cells.

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see “Batch Insertion and Deletion of Rows and Sections” in Table View Programming Guide for iOS.

Availability Available in iOS 2.0 and later.

From docs

First you need to generate the indexPaths array. To generate them you can use

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

Docs here

And you must update your dataSource array and the numberOfRowsInSection must return the correct value [old value + numberOfAddedRows];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM