0

我有一个UIViewController,其中有一个

 @interface MYViewController : UIViewController<UITableViewDelegate>

  @property (weak, nonatomic) IBOutlet UITableView *tblView;

现在我正在实现 UITableViewDelegate,因为我想使用 tableview。

所以在我的viewdidload我做

- (void)viewDidLoad
  {
   [super viewDidLoad];
    self.tblView.delegate = self;
    //self.tblView.dataSource = self;
  }

现在Datasource设置会发出警告,而我从未点击过

cellForRowAtIndexPath

方法。

对编辑感到抱歉。

我究竟做错了什么?

4

5 回答 5

3

你需要

self.tblView.datasource = self;

也通过UITableViewDatasource协议,

@interface MYViewController : UIViewController<UITableViewDelegate,UITableViewDatasource>

cellForRowAtIndexPath是一种数据源方法。

于 2013-07-10T07:22:13.213 回答
0

你的接口也应该实现UITableViewDataSource协议。

@interface MYViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> 
{ 
....
}
于 2013-07-10T07:20:28.463 回答
0

请尝试如下.. UIViewController.h

@interface MYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
  @property (Strong, nonatomic) IBOutlet UITableView *tblView;

请不要忘记将 tblView 出口连接到 .xib 文件中的 tableView,因为这是非常必要的,否则您的数据源方法将不会被调用。

在 UIViewController.m 文件中

  - (void)viewDidLoad {  [super viewDidLoad];
self.tblView.delegate = self;
self.tblView.dataSource = self; }

并实现两个必需的数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return (number of row you want in table)}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return cell }

当然这对你有用。

于 2013-07-10T07:32:24.923 回答
0

对于 UITableViewDatasource 协议添加UITableViewDatasource

@interface MYViewController : UIViewController<UITableViewDelegate, UITableViewDatasource>

并将这一行添加到viewDidLoad

self.tblView.datasource = self;

并转到您的故事板并将“数据源”和“委托”链接到您的tblView

于 2013-07-10T07:41:06.967 回答
0

您的实施中有几个问题。

  1. 您不符合 UITableViewDatasource 协议。
  2. 您没有设置 tableview 数据源。
  3. 您没有实现 UITableViewDatasource 协议所需的方法

如果不设置数据源,您不能期望工作。

于 2013-07-10T08:02:46.797 回答