16

テーブル ビューにデータを入力する方法についていくつかのチュートリアルを検索してみましたが、古い古いビデオしか見つかりませんでした。少し最近のものからやってみましたが、うまくいきません。私は持っている

- (void)viewDidLoad
{
    [super viewDidLoad];
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [chemarray addObject:@"2"];
    [chemarray addObject:@"test"];
    [chemarray addObject:@"3"];
    [chemarray addObject:@"science"];  
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"     forIndexPath:indexPath];

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

このチュートリアルによると、実行すると、配列に項目が表示されるはずですが、私にとってはそうではありません! これを修正する方法を知っている人はいますか?

4

4 に答える 4

24

セル識別子のnib/classを登録するのを忘れました。

AppleのUITableViewドキュメントを作成します。

重要:メソッドを呼び出す前に、 registerNib:forCellReuseIdentifier:or メソッドを使用してクラスまたはnibファイルを登録する必要があります 。registerClass:forCellReuseIdentifier:dequeueReusableCellWithIdentifier:forIndexPath:

プレーンの例を次に示しますUITableViewCell

- (void) viewDidLoad {
   [super viewDidLoad];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

nib / classを登録しない場合、メソッドdequeueReusableCellWithIdentifier:forIndexPath:は基本的にと同じdequeueReusableCellWithIdentifier:です。また、インスタンスが自動的に作成されることはありません。

于 2012-10-25T07:08:55.020 に答える
3

私は同じ問題に遭遇しました。iOS6 を使用している場合は、ストーリーボードを使用していて、View Controller をカスタム クラスに関連付けたと仮定します。

解決策は、ストーリーボードに移動し、テーブルビューのセルのプロパティに移動して、識別子の値を「セル」に変更することです。クラスを ViewDidLoad に登録する必要はありません。

于 2013-01-24T02:23:01.027 に答える
1
  1. デリゲートをuitableview.delegate = self;に設定します。
  2. 宣言プロトコルにあるかどうかを確認してください <UITableViewDelegate, UITableViewDataSource>
  3. ストーリーボードを使用しない場合は、cell==nilの場合は必ずセルを初期化してください。

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
     if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
     }
    
于 2012-10-25T07:08:18.920 に答える
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = [chemarray objectAtIndex:indexPath.row];
    return cell;
}

UITableViewDataSourceがにあり、デリゲート、データ ソース、セルが に接続されていることを確認してUITableViewDelegateください。.htableviewcellindentifier.xib

于 2013-07-19T16:41:23.150 に答える