简体   繁体   English

如何使用平滑滚动向UItableview添加新行

[英]how to add new row to UItableview with smooth scrolling

i am adding some images and messages getting from .net web server. 我正在添加一些从.net web服务器获取的图像和消息。

i am refreshing the data for every 3 sec. 我每3秒刷新一次数据。

After 3 sec i am getting new messages.I need to add these messages and images at the top of the UITableview. 3秒后,我收到新消息。我需要在UITableview的顶部添加这些消息和图像。

like messages adding in twitter, google realtime . 喜欢在Twitter上添加消息, 谷歌实时

for that some where i found insertRowsAtIndexPaths:withRowAnimation: rather then reloading the table view. 对于那些我在哪里找到insertRowsAtIndexPaths:withRowAnimation:而不是重新加载表视图。

but i did n't understand how can i use this insertRowsAtIndexPaths:withRowAnimation: method. 但我不明白我怎么能使用这个insertRowsAtIndexPaths:withRowAnimation:方法。

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: . 您只需创建一个数组,其中包含要插入的新行的索引路径( +[NSIndexPath indexPathForRow:inSection:] ),然后将该数组传递给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. 然后,表视图将调用适当的数据源方法( tableView:cellForRowAtIndexPath:等),以向您的数据源询问新行的数据。 So the moment you call insertRowsAtIndexPaths:... , your data source must already contain the data for the new rows. 因此,当您调用insertRowsAtIndexPaths:... ,您的数据源必须已包含新行的数据。 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. insertRowsAtIndexPaths:withRowAnimation:在接收器中插入由索引路径数组标识的位置的行,并提供动画插入的选项。

  • (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. (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation参数indexPaths NSIndexPath对象的数组,每个对象表示行索引和段索引,它们一起标识表视图中的行。 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. 讨论UITableView会立即调用相关的委托和数据源方法,以获取可见单元格的单元格和其他内容。

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. 请注意在beginUpdates和endUpdates方法定义的动画块中调用此方法时的行为。 UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. UITableView延迟任何行或节的插入,直到它处理了行或节的删除。 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. 可用性适用于iOS 2.0及更高版本。

From docs 来自docs

First you need to generate the indexPaths array. 首先,您需要生成indexPaths数组。 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]; 并且您必须更新您的dataSource数组,并且numberOfRowsInSection必须返回正确的值[old value + numberOfAddedRows];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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