0

現在のプロジェクトで単純な TableView を作成しようとしています。しかし、IBでデータソースに接続してiOSシミュレーターを実行した後、このエラーが発生しました。

2013-03-22 14:58:32.372 M[1875:c07] -[ViewController tableView:numberOfRowsInSection:]:      
unrecognized selector sent to instance 0x88191f0
2013-03-22 14:58:32.374 M[1875:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[ViewController tableView:numberOfRowsInSection:]:        
unrecognized selector sent to instance 0x88191f0'

、しかし、同じコードで空のプロジェクトに単純なTableViewを作成すると、機能します。

これが私のコード「MCEventActivityViewController.h」です

#import <UIKit/UIKit.h>

@interface MCEventActivityViewController : UIViewController <UITableViewDelegate,   UITableViewDataSource>

@end

「MCEventActivityViewController.m」

#import "MCEventActivityViewController.h"

@interface MCEventActivityViewController ()

@end

@implementation MCEventActivityViewController
{
    NSArray *tableData;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tableData = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}

@end

このエラーが発生した理由を理解するのを手伝ってくれる人はいますか?

スクリーンショットはこちら。 ここに画像の説明を入力

ここに画像の説明を入力

編集: ここに画像の説明を入力

4

3 に答える 3

3

確実に、

1.TableView は XIB で FileOwner と接続されます。

2.@interface Controller : UIViewController UITableViewDelegate、UITableViewDataSource が含まれています。

3. Delegate&DataSource両方とも FileOwner に接続されています。

4.numberOfSectionsリターン 1.

5.numberOfRowsInSection返品【必須】。

6. cellForRowAtIndexPath { ここにデータが入ります... }

編集:

@property (nonatomic,strong) IBOutlet UITabelView * tableView; を作成します。

Xibのこのアウトレットをファイル所有者に接続します

于 2013-03-22T08:43:46.027 に答える
0

numberOfSectionsInTableViewメソッドをコードに追加する

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

NSArray *tableData;に追加MCEventActivityViewController.h

適切に設定し@propertyます。@synthesize

そして書く

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
}
于 2013-03-22T08:26:05.313 に答える