[英]IOS: How to convert NSArray to NSIndexPath
I want to do a batch insertion into UITableview. 我想批量插入UITableview。
MyArray.all() do |datas|
self.tableView.beginUpdates
self.tableView.insertRowsAtIndexPaths(datas, withRowAnimation: UITableViewRowAnimationFade)
self.tableView.endUpdates
self.myarrayvar += datas
end
'myarrayvar' is an array of MyArray. “ myarrayvar”是MyArray的数组。 I tried to refer to apple documentation
我试图参考Apple文档
and stackoverflow which is similar to my question. 和stackoverflow这类似于我的问题。
But I really no idea how to convert my datas, to fit into the parameter of insertRowsAtIndexPaths function. 但是我真的不知道如何转换我的数据以适合insertRowsAtIndexPaths函数的参数。
Using rubymotion 使用rubymotion
Here's one way that I'm currently using: 这是我目前使用的一种方式:
def insertCellsAtBottom(newData)
# Get count for the existing data you have.
currentCount = @data.count
# Build the indexPaths for the new data, adding to the current count each time.
# This will make sure you are creating the correct indexes.
indexPaths = newData.map.with_index do |obj, index|
NSIndexPath.indexPathForRow(currentCount + index, inSection: 0)
end
# Update your datasource and insert the rows.
self.tableView.beginUpdates
@data.addObjectsFromArray(newData)
self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimationFade)
self.tableView.endUpdates
end
Your MyArray
contains an array of objects. 您的
MyArray
包含对象数组。 The method insertRowsAtIndexPaths:withRowAnimation
is for telling the tableView that you have added items to your datasource and that it should go and ask for that data at the indexPaths you supply. insertRowsAtIndexPaths:withRowAnimation
方法用于告知tableView您已向数据源中添加了项目,并且应该去该项目并在您提供的indexPath处请求该数据。
The steps you take are: 您采取的步骤是:
So for example if you start with an array and add one object your would need to do 因此,例如,如果您以数组开头并添加一个对象,则需要执行此操作
# Start with empty data model
my_array = []
# Update the model to have one object
my_array << my_new_object
# Tell the table that you have updated your model by inserting one object at the beginning of the first section
inserted_index_paths = [ NSIndexPath.indexPathForRow(0, inSection: 0]) ]
self.tableView.insertRowsAtIndexPaths(inserted_index_paths, withRowAnimation: UITableViewRowAnimationFade)
If you then inserted an object at the end again it would look very similar except that the newly inserted index path would be NSIndexPath.indexPathForRow(1, inSection: 0)
as you would now be telling the tableView that you inserted a second object into the first section 如果您随后再次在末尾插入一个对象,则看起来非常相似,只是新插入的索引路径为
NSIndexPath.indexPathForRow(1, inSection: 0)
因为您现在告诉tableView您已将第二个对象插入到第一部分
You should do something like 你应该做类似的事情
index_paths = MyArray.map { |index| NSIndexPath.indexPathForRow(index, inSection: 0) }
self.tableView.beginUpdates
self.tableView.insertRowsAtIndexPaths(index_paths, withRowAnimation: UITableViewRowAnimationFade)
self.tableView.endUpdates
in obj-c this is the way to do it... if you get your datas from an NSMutableArray, just make your operation in it. 在obj-c中,这是这样做的方法...如果您从NSMutableArray获取数据,则只需在其中进行操作即可。
for example, i got 例如,我得到了
array = @[@"text1", @"text2", @"text4"];
for my cell, i'm calling in cellForRowAtIndexPath method: 对于我的单元格,我正在调用cellForRowAtIndexPath方法:
[cell setText:array[indexPath.row]];
in my array i'm making : 在我的阵列中,我正在:
array[2] = @"text3";
and then : 接着 :
[self.tableView reloadData];
I didn't test this but it's the good way to insert a new row. 我没有对此进行测试,但这是插入新行的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.