how to set a tableview delegate(如何设置表格视图委托)
问题描述
我正在尝试使用 UITableView 而不使用 nib 并且不使用 UITableViewController.
我已将 UITableView 实例添加到 UIViewController Like So
mytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)];[mytable setDelegate:self];[self.view mytable];我还在 UIViewController 中添加了以下表格视图方法(为简洁起见而删减)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {我收到一条警告说我的 UIViewController 没有实现 UITableView 委托协议.
告诉表格视图其委托方法在哪里的正确方法是什么?
(这是我第一次尝试使用 UITableView 而不从新文件选项中选择 UITableTableview 控制器)
您需要使您的类符合 UITableViewDelegate 和 UITableViewDataSource 协议.( cellForRowAtIndexPath: 在 UITableViewDataSource 协议中)
通过在类接口定义中使用尖括号来做到这一点:
@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{...}您现在而不是之前使用 UITableViewController 时收到此警告的原因是因为 UITableViewController 已经符合这些协议.
所以,本质上一个 当然,如果你还没有继承 I'm trying to use a I have added a Also I have added the following table view methods to my I am getting a warning saying that my Whats the correct way to tell the table view where its delegate methods are? (This is my first attempt at trying to use a You need to conform your class to the Do this by using angle brackets in your class interface definition: The reason why you are getting this warning now and not before when you were using a So, in essence a Of course, if you're not already subclassing
这篇关于如何设置表格视图委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!UITableViewController只是一个符合UITableViewDelegate和UITableViewDataSource的类,并且有一个UITableView代码>实例成员.差不多就这些了.</p>UITableViewController,那么你需要手动设置的:dataSource和delegate>UITableView[tableView setDelegate:self];[tableView setDataSource:self];UITableView without using a nib and without using a UITableViewController.UITableView instance to a UIViewController Like Somytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)];
[mytable setDelegate:self];
[self.view mytable];
UIViewController (cut for brevities sake)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController does not implement UITableView delegate protocol.UITableView without selecting the UITableTableview controller from the new file options)UITableViewDelegate and UITableViewDataSource protocols. ( cellForRowAtIndexPath: is in the UITableViewDataSource protocol )@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}
UITableViewController is because UITableViewController already conforms to these protocols.UITableViewController is just a class that conforms to UITableViewDelegate and UITableViewDataSource, and has a UITableView instance member.
That's pretty much it.UITableViewController, then you need to manually setup the dataSource and delegate of the UITableView:[tableView setDelegate:self];
[tableView setDataSource:self];
本文标题为:如何设置表格视图委托
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01
