未能正确声明/实现 UITableViewDataSource 方法 [英] Failing to properly declare/implement UITableViewDataSource methods
问题描述
我有一个 UIViewController
,我想用它来实现 UITableViewDataSource
的方法.我有这个标题,FriendsController.h
:
I have a UIViewController
that I want to use to implement the methods of UITableViewDataSource
on. I have this header, FriendsController.h
:
#import <UIKit/UIKit.h>
@interface FriendsController : UIViewController <UITableViewDataSource>
@end
现在将@interface
声明更新为:
@interface FriendsController : UIViewController <UITableViewDataSource, UITableViewDelegate>
和这个实现,FriendsController.m
:
#import "FriendsController.h"
@implementation FriendsController
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"CALLED .numberOfRowsInSection()");
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"CALLED cellForRowAtIndexPath()");
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"FriendCell"];
cell.textLabel.text = @"Testing label";
return cell;
}
@end
运行时,这给了我一个-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81734d0
".谁能看看我对 .numberOfRowsInSection()
的实现/声明是否有问题?
When run this gives me a '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81734d0
'. Can anyone see if there is something wrong with my implementation/declaration of .numberOfRowsInSection()
?
我从 here 并运行视图未连接",它输出以下列表:
I've added a technique for method listing from here and run the view 'unconnected', it outputs the following list:
[<timestamp etc>] Method no #0: tableView:numberOfRowsInSection:
[<timestamp etc>] Method no #1: tableView:cellForRowAtIndexPath:
[<timestamp etc>] Method no #2: numberOfSectionsInTableView:
[<timestamp etc>] Method no #3: tableView:didSelectRowAtIndexPath:
[<timestamp etc>] Method no #4: viewDidLoad
发布脚本:@ThisDarkTao 和 @Pei 都做对了,从我之前记录视觉部分的问题中可以看出,此处.
Post scriptum: Both @ThisDarkTao and @Pei got it right, as can be seen in my previous question documenting the visual parts, here.
推荐答案
需要在接口文件中的协议列表中添加UITableViewDelegate
,像这样:<UITableViewDataSource, UITableViewDelegate>
You need to add UITableViewDelegate
to the list of protocols in the interface file, like this: <UITableViewDataSource, UITableViewDelegate>
您还需要以下所有委托方法:
You also need all of the following delegate methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1; // Number of rows
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Test Cell";
return cell;
}
在您的 xib/storyboard 视图中,您还需要将 tableview 的委托和数据源连接连接到 viewcontroller.
In your xib/storyboard view, you also need to connect the tableview's delegate and datasource connections to the viewcontroller.
如果你想让你的单元格在你点击它们时做某事",你还需要实现以下委托方法:
If you want your cells to 'do something' when you tap them, you also need to implement the following delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Tapped cell %d",indexPath.row);
}
这篇关于未能正确声明/实现 UITableViewDataSource 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!