需要帮助理解 NSDictionary 对象
我试图围绕 NSDictionary 类进行思考,以使我的 TableView 发挥作用。 有谁能够为我分解并解释以下代码中发生的情况吗?干杯
if (!self.tableDataSource)
{
self.tableDataSource = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"MenuData" ofType: @"plist"]];
self.navigationItem.title = @"Menu";
}
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Children"] objectAtIndex: indexPath.row];
im trying to wrap my head around the NSDictionary class, to make my TableView functional.
Is anyone able to break down and explain whats happening in the following code for me? cheers
if (!self.tableDataSource)
{
self.tableDataSource = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"MenuData" ofType: @"plist"]];
self.navigationItem.title = @"Menu";
}
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Children"] objectAtIndex: indexPath.row];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为如果您只是解压所有方法调用以查看各种对象的隐含嵌套,那么代码本身就相当清晰了。评论:
我认为苹果文档 TableDataSource< /a> 可能会为您提供有关此代码正在执行的操作的更大上下文。特别是,字典、数组、字典、数组的嵌套(这就是最后一行中发生的情况)可能是对行、列等的所有信息进行编码的标准方法,例如数据驱动 UITableView。
I think the code itself is fairly clear if you just unpack all the method calls to see the implied nesting of various objects. Comments:
I think the Apple docs on TableDataSource will probably give you the larger context on what this code is doing. In particular, the nesting of a dictionary, in an array, in a dictionary, in an array, which is what's going on in the last line, may be a standard way of encoding all the info for rows, columns, etc., for data driving a UITableView.
您应该将 DataSource 和 Delegate 设置为指向 tableView 的所有者,然后实现 UITableViewDelegate 函数以正确设置表的内容。
编辑:
如果您计划实现带有部分的表视图,我建议您使用 MxN 数据结构:NSArray of NSArray,这样您就可以通过以下方式轻松访问单元格数据:
You should set the DataSource and Delegate to point to the tableView's owner and then implement the UITableViewDelegate functions to correctly set the content of the table.
Edit:
If you're planning on implementing a table view with sections, I advise you to use a MxN datastructure of: NSArray of NSArray, that way you will be able to access vey easily your cell data with:
NSDictionary
对象存储键引用的其他对象。键是用于引用字典中的对象的唯一字符串。对象可以是任何对象。 NSMutableDictionary 是相同的东西,但允许在创建字典后编辑(添加/删除/更改)键对象。将 NSDictionary(或任何不支持 UITableViewDataSource 协议的类)设置为表视图的
dataSource
属性是不正确的。最常见的是,您的视图控制器是UITableViewController
子类,或者是支持UITableViewDelegate
或UITableViewDataSource
的UIViewController
子类。财产。这就是你问题的直接答案。为了更全面地理解,您应该返回并阅读 Apple 的文档,以确保您理解我的答案。表视图控制器编程指南将向您展示如何在视图控制器中实现表视图。对于您制作的其他东西,需要有关 Objective-C 和其他 iOS 对象的信息。
NSDictionary
objects store other objects referenced by keys. A key is a unique string used to reference the object in the dictionary. An object can be any object. AnNSMutableDictionary
is the same thing but allows editing (adding/removing/changing) objects for keys after the dictionary has been created.It is incorrect to set an NSDictionary (or any class that does not support the
UITableViewDataSource
protocol) to thedataSource
property of a table view. Most commonly, your view controller is aUITableViewController
subclass or it is aUIViewController
subclass that supports theUITableViewDelegate
orUITableViewDataSource
property.That's the direct answer to your question. For a fuller understanding you should go back and read Apple's docs to make sure you understand my answer. The Table View Controller Programming Guide will show you how to implement a table views in a view controller. For other things you made need info on Objective-C and other iOS objects.