gpt4 book ai didi

iOS UITableView - 从 NSDictionary 中进行多项选择

转载 作者:行者123 更新时间:2023-11-29 02:57:48 25 4
gpt4 key购买 nike

描述:

我有一个 UITableView,每行都加载了 Facebook 好友 json 数据。用户将选择多个 friend ,当按下“完成”按钮时,他们选择的内容将填充到上一页的标签中。

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?

}

问题:

我不清楚如何处理选定的索引。我知道我排序的 friend 列表在“friendsDict”字典中,但如何使用“selectedIndexes”数组从字典中获取所选 friend 的“id”?

我失败的尝试:

-doneButtonPushed {

selectedIndexes = [self.tableView indexPathsForSelectedRows];


//this does not work
NSMutableArray *friendsArray = [friends objectsAtIndexes:selectedIndexes];

}

我已经为此挠头两天了,这真的让我脑袋里塞满了椒盐卷饼。我什至不知道该怎么想了。请各位码神帮忙!

最佳答案

您的代码的主要问题是 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 ...
}

关于iOS UITableView - 从 NSDictionary 中进行多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23687738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com