循环访问 cellForRowAtIndexPath 处的字典数组
我有一系列字典。每本词典都有一个类别。我想用所有具有一个共同类别的字典填充一个表格视图,该类别是在上一个表格视图中选择的。
NSDictionary *name = [sortedNames objectAtIndex:indexPath.row];
NSMutableString *currentCat = [name objectForKey:@"category"];
if ([currentCat isEqualToString:catSelected]) {
cell.textLabel.text = [name objectForKey:@"title"];
}
正如您可能已经猜到的,如果数组中的前两个字典不是所选类别的类型,但第三个是,则第三个单元格行将被填充 ?
我该如何以正确的方式处理这个问题
I have an array of dictionaries. Each dictionary has one category. I want to fill a tableview with all of the dictionaries with one category in common, which was selected in a previous tableview.
NSDictionary *name = [sortedNames objectAtIndex:indexPath.row];
NSMutableString *currentCat = [name objectForKey:@"category"];
if ([currentCat isEqualToString:catSelected]) {
cell.textLabel.text = [name objectForKey:@"title"];
}
As you could've guessed, if the first two dictionaries in the array are not of the category selected type, but the third one is, then the third cellrow gets filled in.
How do I approach this the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .h 文件中获取一个可变数组在 viewWillAppear init 中并使用您的代码为该数组添加对象,例如
In numberOfRowsInSection 方法给出
In cellForRowAtIndexPath 方法
take a mutable array in .h file In viewWillAppear init and add objects for that array using your code like
In numberOfRowsInSection method give
In cellForRowAtIndexPath method
您可以在加载视图控制器时构造一个
NSMutableArray
。该数组仅包含您想要在该表视图中显示的对象。然后,在
tableView:cellForRowAtIndexPath:
中,根据索引进行分配:这里的关键思想是,我们不会引入表视图不需要的数据。
You could construct an
NSMutableArray
on loading your view controller. This array would only contain the objects you want to display in that table view.Then, in your
tableView:cellForRowAtIndexPath:
, you assign based on the index:The key idea here is that we don't bring data we don't need for the table view.