1

UIViewControlleronのメソッドを実装するために使用したいものがあります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()か?

編集:ここからメソッドリストの手法を追加し、ビュー'unconnected'を実行すると、次のリストが出力されます。

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

スクリプトを投稿する: @ThisDarkTaoと@Peiの両方が正しく理解しました。これは、視覚的な部分を文書化した以前の質問に見られるように、ここにあります。

4

2 に答える 2

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

xib /ストーリーボードビューでは、テーブルビューのデリゲート接続とデータソース接続もビューコントローラに接続する必要があります。

セルをタップしたときにセルに「何かを実行」させたい場合は、次のデリゲートメソッドも実装する必要があります。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Tapped cell %d",indexPath.row);     
}
于 2012-05-28T13:35:53.587 に答える
1

プロジェクトでストーリーボードを使用する場合、ストーリーボードUIViewControllerの Identity Inspector で の Class フィールドを「FriendsController」に設定する必要があります。したがってUIVIewController、正しいクラス (この場合は FriendController) を使用していることを示すことができます。

ペイ。

于 2012-05-28T14:23:10.327 に答える