问题描述
我有一个 UIViewController,它调用 4 个 UITableViewController 作为它的子视图.
I have a UIViewController which calls 4 UITableViewControllers as its subview.
这些 tableview 启用了附件复选标记,可以选择表格中的多行.
These tableview has accessory checkmark enabled and can select multiple rows in a table.
我的问题是当我的 UIViewController 与所有 tableview 一起显示时,我想保存每个 tableview 的选定值并将其保存在数组或其他东西中.
My question is when my UIViewController is displayed with all the tableview, I want to save the selected values of each tableview and save it in an array or something else.
我还想在另一个类中使用这些选定的值,在那里我必须显示所有这些选定的值.
I also want to use these selected values in another class, where I have to display all these selected values.
谁能帮助我解决如何保存不同 tableview 的多个值并在另一个类中使用这些值的问题?
Can anyone help me out with this on how to save multiple values of different tableview and use those values in another class?
推荐答案
只需使用 indexPathsForSelectedRows
.但是这条指令会返回多个indexPath
.
Just use indexPathsForSelectedRows
.But this instruction will return more than one indexPath
.
也就是说,Xcode 具有出色的自动完成功能,您可以开始键入index",它会直接显示 indexPathsForSelectedRows
,您也可以参考 Xcode 提供的优秀文档.希望能帮到你!
That said, Xcode has a brilliant autocompletion feature, you could have started to type "index" and it would have shown indexPathsForSelectedRows
directly, you can also refer to the excellent documentation provided with Xcode. I Hope it will help you!
更新 1 - 如何从数组中获取 indexPaths
indexPathsForSelectedRows
方法将返回一个 NSIndexPath 对象数组,每个对象对应一个选定的项目.如果没有选中的项目,此方法返回一个空数组.
the indexPathsForSelectedRows
method will return an array of NSIndexPath objects, each of which corresponds to a single selected item. If there are no selected items, this method returns an empty array.
现在要访问这个对象,你必须问自己,我如何访问一个数组?你做了一些研究,然后你会得出这个结论:
Now to access this object, you have to ask yourself, how do I access an array? You do some research and then you will come to this conclusion:
NSArray *arrayOfIndexPathsTableOne = [self.myTableViewOne indexPathsForSelectedRows];
//Then you can iterate through the array and doWhatever you like.
for(int i = 0; i < [arrayOfIndexPathsTableOne count]; i++){
NSIndexPath *indexPathImInterestedIn = [arrayOfIndexPathsTableOne objectAtIndex:i];
//If you do actually have a tableViewController then call [self cellFor...]; instead of self.myTableViewOne
UITableViewCell *currentCell = [self.myTableViewOne cellForRowAtIndexPath:indexPathImInterestedIn];
//you can grab the data you need from this cell and do whatever you like
//I would suggest you grab the data from the datasource instead of the cell, since your datasource should contain the datamodel where you can extract the information you need.
//Or if its just one value from the cell you want to grab you could do
[myGlobalArray addObject:[NSString stringWithObject:@"%@", currentCell.textLabel.text]]; //your value
}
//Voila
这篇关于从 UITableView 保存多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!