2011-09-09 13 views
3

In My TableVIew Imロード中のCustomeセルXibでは、CellForIndexPathのすべてのエントリに対して、セルが再作成されます。どのように細胞のレクリエーションを避けるために??テーブルビューCell Recreation時にセルがロードされたとき

Iam new to Iphone、よろしくお願いします。

+0

あなたのcellForRowAtIndexPathメソッドはありますか?だから我々は間違いを見つけることができるでしょう... – Vladimir

+0

投稿コードが役に立ちます! – mayuur

答えて

1

Nibからビューをロードすると、cellForRowAtIndexPathが呼び出されるたびにメモリが割り当てられますので、毎回メモリを割り当てる代わりにセルを再描画する方が良い方法です。 以下の例が役立ちます。

カスタムセルのパラメトリッククレートラベル。 like

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 
lblusername=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 150, 25)]; 
    lblusername.backgroundColor=[UIColor clearColor]; 
    lblusername.textColor=[UIColor colorWithRed:33.0/255.0 green:82.0/255.0 blue:87.0/255.0 alpha:1.0]; 
    [contentview addSubview:lblusername]; 
    [lblusername release]; 
} 
return self; 
} 

以下に挙げるコードを使用してカスタムセルを呼び出します。

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

static NSString *CellIdentifier = @"Cell"; 

LeaderboardCustomeCell *cell = (LeaderboardCustomeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[LeaderboardCustomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
[email protected]"Hello"; 
} 
4

標準メソッドを使用して、以前に作成したセルをキャッシュすることができます。あなたの- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法では、次のアプローチを用いて細胞を作成する必要があります。

static NSString *CellIdentifier = @"YourCellIdentifier"; 
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    // create (alloc + init) new one 
    [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];   
    cell = myCell; 
    self.myCell = nil; 
} 
// using new cell or previously created 

は、すべての可視セルのためのメモリ・オブジェクトに格納することを忘れないでください。テーブルをスクロールすると、このセルが再利用されます。

たとえば、10個の目に見えるセルがある場合、cellは== nilで10回になり、+ allocすることになります。下にスクロールすると、もう1つのセルが作成され(11個のセルが表示されるため)、12個のセルの場合、最初のセル用に作成されたセルが再利用されます。

@rckoenesとして、IBのセルの同じCellIdentifierを設定することを忘れないでください。

希望、私は明らかだった。

+2

最も重要なことは、インタフェースビルダーでセルのcellIdentifierを設定することです。 – rckoenes

+0

ありがとう、私の答えを更新しました – Nekto

関連する問題