2013-04-02 7 views
5

UIViewControllerを使用して、ある時点でUITableViewが大きくなり、単純な場合はTableViewインスタンス変数を初期化してビューに追加しますが、ビューに追加するセルのデキューをどのように処理するかはわかりません;再利用識別子が必要ですが、設定方法がわかりません。プログラムでUITableViewを追加する - セルの再利用識別子を設定するにはどうすればよいですか?

この方法ではどうすればよいですか?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    return cell; 
} 
+0

'cellForRowAtIndexPath'の実装で行うことと同じことを行います。テーブルビューを取得した方法については、テーブルビューの動作の仕方が変わりません。あなたが示したコードは完全に良いスタートです。 iOS 5の – matt

答えて

7

cell場合方法initWithStyle:reuseIdentifier

  1. チェックを使用し、それは、その後、あなたはそれを初期化する必要がない場合
  2. が存在します。

コード

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

    if (!cell) 
     cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier]; 

    return cell; 
} 
+0

は '!cell'を確認する必要はありません。 http://stackoverflow.com/questions/7946840/dequeuereusablecellwithidentifier-behavior-changed-for-prototype-cells –

+0

IBとは違って、私はすべてのセルにUIViews(UILabel、UIImageなど)を使用して特定のレイアウトを与えることができます私はUILabelを作成し、各セルのセルのサブビューに追加する必要がありますか? –

+0

UITableViewCellのサブクラスを作成し、そこで特定のサブビュー設定を行うことができます。 – MJN

0

再利用識別子を明示的にあなたが質問に含ま定義は例:

Reference

ため

で動作するのに十分ですcellForRowAtIndexPath方法をdefined.Inする必要はありません

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]]; 
    } 
    Region *region = [regions objectAtIndex:indexPath.section]; 
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row]; 
    cell.textLabel.text = timeZoneWrapper.localeName; 
    return cell; 
} 
関連する問題