未能正确申报/实施UITableViewDataSource方法(Failing to proper

2019-10-16 18:39发布

我有一个UIViewController ,我要用来实现的方法UITableViewDataSource上。 我有这个头, 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()

编辑:我从添加方法上市的技术在这里并运行视图“无关”,它输出以下列表:

[<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

邮政scriptum:两个@ThisDarkTao和@Pei得到它的权利,如可以在我刚才的问题中可以看出记录的视觉部分, 在这里 。

Answer 1:

您需要添加UITableViewDelegate到协议列表在接口文件中,就像这样: <UITableViewDataSource, UITableViewDelegate>

您还需要以下所有委托方法:

- (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;
}

在您的厦门国际银行/故事板视图,您还需要将tableview中的委托和数据源连接线连接到ViewController。

如果你想你的细胞,以“做一些事情”,当你点击它们,你还需要实现以下的委托方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Tapped cell %d",indexPath.row);     
}


Answer 2:

如果你在项目中使用的故事板,你必须设置UIViewController的类字段是你在你的故事板的身份检查‘FriendsController’。 所以,你可以提出你UIVIewController使用了正确的类(在这种情况下,FriendController你)是。

Pei.



文章来源: Failing to properly declare/implement UITableViewDataSource methods