0

UITableView をプログラムで実装しようとしています。

UIView(IndexViewという名前)があります。コンストラクタは次のとおりです。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        MyTableViewController* favorite_table_view_controller = [[MyTableViewController alloc] init];
        UITableView* theTableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 65, 280, 350) style:UITableViewStylePlain];
        theTableView.delegate = favorite_table_view_controller;
        theTableView.dataSource = favorite_table_view_controller;
        [self addSubview:theTableView];
    }
    return self;
}

MyTableViewController は次のように定義されています。

@interface MyTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

そして、2 つの必要な機能を実装します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];

    [cell.textLabel setText:[data objectAtIndex:indexPath.row]];

    return cell;
}

そして、私はこのエラーが発生します:

[__NSMallocBlock__ tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6897de0
2012-07-18 12:07:24.607 ButtonsTest[22854:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6897de0'

コントローラーをビューにバインドするときに問題があると思います。しかし、私はどこを見つけることができません。

問題はどこから来たのですか?

4

1 に答える 1

0

ビューからビューコントローラを構築しないでください。逆に行う必要があります(UIVIewControllersからビューを作成します)

サブクラス化するViewControllerを作成してUIViewControllerから、とを実装しUITableViewDelegateますUITableViewDataSource。それでも別のコントローラクラスからUITableViewを制御する必要がある場合はUIViewController、ビュー自体からではなく、親でこの接続を設定します。これが、iOSで集中的に使用されるMVCの基本的な前提です。

于 2012-07-18T10:33:19.177 に答える