57

私はアップルのドキュメントを読みましたが、私のような初心者には理解できObjective-Cません。UITableViewこの リンクの例に従って複数列を実装しようとしていますが、うまくいかないのでcellForRowAtIndexPath、どのように機能するかを理解する必要があります。個人的には、この方法はかなり複雑に思えます。

1) それは何を返しますか? UITableViewCell? しかし、なぜそれはとても奇妙に見えるのですか?

-(UITableViewCell *)tableView:(UITableView *)tableView 
  • それは何ですか?説明していただけますか?

2) どのように呼び出され、さらに重要なことは、どのように特定のものに接続するのですかUITableView??? UITableView2 つの名前のfirstTableViewandsecondTableViewがあり、それらを異なるものにしたい場合 (異なるパフォーマンスを行うため)はどうすればよいcellForRowAtIndexPathですか? どのように私UITableViewsをこれにリンクすることになっていますか

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

NSIndexPathメソッドはではなくを受け入れますUITableView。私は何をするつもりですか?

4

3 に答える 3

96

私はそれを分解しようとします(ドキュメントの例)

/* 
 *   The cellForRowAtIndexPath takes for argument the tableView (so if the same object
 *   is delegate for several tableViews it can identify which one is asking for a cell),
 *   and an indexPath which determines which row and section the cell is returned for. 
 */ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    /*
     *   This is an important bit, it asks the table view if it has any available cells
     *   already created which it is not using (if they are offScreen), so that it can
     *   reuse them (saving the time of alloc/init/load from xib a new cell ).
     *   The identifier is there to differentiate between different types of cells
     *   (you can display different types of cells in the same table view)
     */

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    /*
     *   If the cell is nil it means no cell was available for reuse and that we should
     *   create a new one.
     */
    if (cell == nil) {

        /* 
         *   Actually create a new cell (with an identifier so that it can be dequeued). 
         */

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    /*
     *   Now that we have a cell we can configure it to display the data corresponding to
     *   this row/section
     */

    NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
    cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;

    /* Now that the cell is configured we return it to the table view so that it can display it */

    return cell;

}

これはメソッドであるため、のDataSourceとして自身を宣言したオブジェクトで呼び出されます。これは、テーブル ビューが実際に画面上にセルを表示する必要があるときに呼び出されます。これは、行とセクションの数 (他の DataSource メソッドで指定) に基づいています。DataSourceUITableView

于 2011-11-10T12:49:45.520 に答える
38

1) 関数は、テーブル ビューのセルを返します。はい? したがって、返されるオブジェクトのタイプはUITableViewCellです。これらは、テーブルの行に表示されるオブジェクトです。この関数は基本的に、テーブル ビューのセルを返します。しかし、2 番目の質問で答えられるように、関数がどの行に対してどのセルを返すかをどのように知るのでしょうか。

2)NSIndexPath本質的に2つのことです-

  • あなたのセクション
  • あなたの列

テーブルは多くのセクションに分割され、それぞれに独自の行があるため、これにより、NSIndexPathどのセクションとどの行を正確に識別することができます。どちらも整数です。初心者の方は、1 つのセクションだけで試してみてください。

UITableViewDataSourceView Controllerにプロトコルを実装すると呼び出されます。より簡単な方法は、UITableViewControllerクラスを追加することです。Apple には、テーブルを記述できる関数を簡単に実装するためのコードが書かれているため、これを強くお勧めします。とにかく、このプロトコルを自分で実装することを選択した場合は、UITableViewCellオブジェクトを作成し、それを任意の行に返す必要があります。テーブル ビューに表示されるセルは何度も再利用されるため、再利用性を理解するには、そのクラス リファレンスを参照してください (これは非常に効率的な設計です)。

2 つのテーブル ビューがある場合は、メソッドを参照してください。テーブルビューが渡されるので、それに関しては問題ありません。

于 2011-11-10T12:50:15.030 に答える