添加UITableView作为子视图

Kyl*_*ard 1 cocoa-touch objective-c uitableview ios unrecognized-selector

我正在尝试创建一个视图,其中只有UITableView作为该视图的一部分.我相信这是从代码(而不是界面构建器)创建时的正确模式,但如果我的方法也是错误的,请随意添加建议.

我得到的例外是: [KBSMoreViewController tableView:numberOfRowsInSection:]:发送到实例的无法识别的选择器

我有一个类标题如下(我在实现中实现构造函数):

#import <UIKit/UIKit.h>
@interface KBSMoreTableView : UITableView 
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
@end
Run Code Online (Sandbox Code Playgroud)

然后我有一个ViewController类头:

@interface KBSMoreViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
Run Code Online (Sandbox Code Playgroud)

ViewController是标签栏的一部分,工作正常(我试图添加的tableview之外)实现如下:

#import "../Models/KBSMoreTableView.h"

@interface KBSMoreViewController ()
@property (strong, nonatomic) KBSMoreTableView* tableView;
@property (strong, nonatomic) NSString* cellIdentifier;
@property (copy, nonatomic) NSArray *source;

@end

@implementation KBSMoreViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
        self.source = [[NSArray alloc] initWithObjects:@"Test1", @"Test2", nil];
        self.cellIdentifier = @"MoreCellId";
    }
    return self;
}


- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return self.source.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
    cell.textLabel.text = self.source[indexPath.row];
    return cell;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[KBSMoreTableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 7

错误很明显.您没有实现tableView:numberOfRowsInSection:表视图数据源方法.相反,您已经创建了一个名为的方法numberOfRowsInSection:.

改变这个:

- (NSInteger)numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

至:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)