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];
本文标题为:如何设置表格视图委托
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
