如何在 .xib 文件上创建的 UIViewController 中设置 UITableView [英] How to set up UITableView within a UIViewController created on a .xib file

查看:21
本文介绍了如何在 .xib 文件上创建的 UIViewController 中设置 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课:

@interface ExerciseLogDetails : UIViewController<UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource> {

我试图在其中显示一些元素,然后是 UITextView.UITextView 元素是在 Interface Builder 上创建的.执行此代码时:

where I am trying to display some elements followed by a UITextView. The UITextView element is created on Interface Builder. When executing this code:

- (void)viewDidLoad {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds       style:UITableViewStylePlain];
tableView.dataSource = self; 
tableView.delegate = self;
[self.view addSubview:self.tableView];
}

显示的是一张表格,但不是我在 Interface Builder 中配置的表格.它是完全空白且未格式化的.如何访问我的表并以编程方式用数据填充它?

a table shows, but not the one I configured in Interface Builder. It is completely blank and unformatted. How can I access my table and populate it progrmmatically with data?

谢谢!

推荐答案

如果你在 IB 中配置了一个 tableView 你不应该也以编程方式创建一个,你应该创建 @property (nonatomic, retain) IBOutlet UITableView *tableView; 并将其连接到您在 IB 中配置的 tableView.
尝试在tableView的
中设置断点- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
委托方法来查看这个方法是否被调用.

If you configured a tableView in IB you shouldn't also create one programmatically, you should create @property (nonatomic, retain) IBOutlet UITableView *tableView; and connect it to the tableView you configured in IB.
Try to set a breakpoint in the tableView's
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
delegate method to see if this method get called.

来自 Apple UITableView 文档:

From Apple UITableView docs:

一个 UITableView 对象必须有一个作为数据源的对象和一个充当委托的对象;通常这些对象是应用程序委托或更常见的自定义UITableViewController 对象.数据源必须采用UITableViewDataSource 协议和委托必须采用UITableViewDelegate 协议.数据源提供信息UITableView 需要建表和管理数据模型当表的行被插入、删除或重新排序时.代表提供表格使用的单元格并执行其他任务,例如管理附件视图和选择.

A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.

正如你所看到的,如果你没有为 tableView 设置 dataSource,tableView 将不知道如何显示以及显示什么,所以什么都不会发生.
您可以通过调用 tableView.dataSource = self; 或在 IB 中从 tableView 拖动到文件的所有者(即必须实现 UITableViewDataSource 协议的viewController)来设置一个

As u can see if u don't set a dataSource to your tableView, the tableView will not know how and what to display, so nothing will happen.
You can set one by calling tableView.dataSource = self; or in IB drag from your tableView to the file's owner (that is your viewController that must implement the UITableViewDataSource Protocol)

UITableViewDataSource 协议中有两个方法是你的数据源必须实现的:

There are two methods in the UITableViewDataSource protocol that your dataSource must implement:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath  

如果您不实施这些方法,您将收到编译器警告.
如果你实现了 UITableViewDelegate 协议,你可以更好地控制 tableView 的外观——比如行/页眉/页脚高度、选择等等......

If u won't implement those methods u will get a compiler warnings.
You can have more control on how the tableView will look if you implement the UITableViewDelegate protocol - like row/header/footer height, selections and more...

来自 Apple UITableView 文档:

From Apple UITableView docs:

UITableView 覆盖了 UIView 的 layoutSubviews 方法,使其仅在创建 UITableView 的新实例时调用 reloadData 或当您分配新的数据源时.重新加载表视图清除当前状态,包括当前选择.然而,如果你显式调用 reloadData,它清除此状态和任何后续直接或间接调用 layoutSubviews 不会触发重新加载.

UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData, it clears this state and any subsequent direct or indirect call to layoutSubviews does not trigger a reload.

ReloadData 在创建 tableView 或分配新数据源时(或当您当然明确调用它时..)时被调用.
这是 tableView 需要知道要显示什么(多少节?多少行?以及要显示哪个单元格?)的时候 - 所以这是调用 numberOfRowsInSextion 方法的时候.

ReloadData get called when the tableView is created or when you assign a new dataSource (or when you explicitly call it of course..).
This is when the tableView needs to know what to display (how many sections?, how many rows?, and which cell to display?) - So this is when numberOfRowsInSextion method called.

这篇关于如何在 .xib 文件上创建的 UIViewController 中设置 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆