说明
我有一个UITableView,每行都加载了Facebook好友json数据。用户将选择多个朋友,并且他们的选择内容将填写前一页上的标签,当"完成"按下按钮。
NSMutableArray *friends;
NSDictionary *friendsDict;
NSArray *selectedIndexes;
-viewDidLoad {
//Grab the data from Facebook and put it into an array
friends = [NSMutableArray arrayWithArray:jsonData[@"data"]];
}
-cellForRowAtIndexPath {
friendsDict = friends[indexPath.row];
cell.textLabel.text = friendsDict[@"name"];
}
-doneButtonPushed {
selectedIndexes = [self.tableView indexPathsForSelectedRows];
//NOW WHAT?
}
问题:
我不清楚如何处理所选索引。我知道我订购的朋友名单在" friendsDict"字典,但我如何得到" id"来自词典中选定的朋友使用" selectedIndexes"阵列
我失败的尝试:
-doneButtonPushed {
selectedIndexes = [self.tableView indexPathsForSelectedRows];
//this does not work
NSMutableArray *friendsArray = [friends objectsAtIndexes:selectedIndexes];
}
我已经在这上面翻了两天头,这真的到了我脑袋里的椒盐脆饼。我不知道如何再考虑它。请帮助编码神!
答案 0 :(得分:1)
您的代码的主要问题是NSArray方法objectsAtIndexes:
采用NSIndexSet类型的参数,但UITableview的indexPathsForSelectedRows
为您提供包含NSIndexPath对象的NSArray。以下是您使用NSArray的方法:
- doneButtonPushed
{
selectedIndexes = [self.tableView indexPathsForSelectedRows];
if (nil == selectedIndexes)
{
// No selection
return;
}
// The documentation of indexPathsForSelectedRows talks about
// "index-path objects" - by this it means that the objects
// in the array have the type NSIndexPath
for (NSIndexPath* indexPath in selectedIndexes)
{
// Here we get onto familiar ground :-)
friendsDict = friends[indexPath.row];
// I don't know the type of the "id" object, so I am just
// using the generic type id in this example. This is just
// a coincidence and has nothing to do with the key of
// your dictionary being "id". If you know that the "id"
// object is, for instance, an NSString, then change
// the type of the variable friendId to NSString*.
id friendId = friendsDict[@"id"];
// Now do something with friendId ...
}