2012-01-25 9 views
0

他のUITableViewの問題があります。何らかの理由で、indexPath.rowがすべて混乱します。セルを設定するifステートメントをコメントアウトすると、すべて正常に動作します。 NSLogsは、それらが順番にロードされていると私に伝えますが、すべてのセルは順不同です。UITableViewCellsの順序が狂っています

また、繰り返しているようです。私は8個の細胞しか見ることができず、何度も何度も繰り返します。ここで

は私のコードです:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 
NSLog(@"row: %d",indexPath.row); 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.contentView.backgroundColor = [UIColor clearColor]; 

    // Add subviews like this: 
    // [[cell contentView] addSubview:objectName]; 
    // And I get the row number like this: indexPath.row when getting objects from the array 

} 

return cell; 
} 

答えて

1

あなたのコードを使用するには: "私は唯一の8つのセルを参照してください、そして、彼らは何度も繰り返し"

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

static NSString *CellIdentifier = @"Cell"; 
NSLog(@"row: %d",indexPath.row); 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor = [UIColor clearColor]; 
     cell.contentView.backgroundColor = [UIColor clearColor]; 

     // Add subviews like this: 
     // [[cell contentView] addSubview:objectName]; 


    } 
    ### Move this here ### 
// And I get the row number like this: indexPath.row when getting objects from the array 
return cell; 
} 

を正しい。

あなたの欠けていることは、それがどのように働くのかということです。それで、セルが無ければ、新しいセルを初期化するのに&を割り当てます。したがって、あなたはallocとinitを行い、色を設定し、ifステートメントにサブビューを追加します。その後、if(cell==nil)の後、有効なセルがあり、indexPath変数が渡されます。

セルがnilで表示されているすべてのデータを割り当てているという問題があります。 indexPathによると、問題は、データが変更されないように、セルが2回目に使用されていないということです。

さらにスピードのコメントに対処するため、古いフォールバックの例を使用します。

- (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]; 
     UILabel *hugeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
     hugeLabel.tag = 300; 
     [cell addSubview:hugeLabel]; 
    } 
    [(UILabel *)[cell viewWithTag:300] setText:[arrayOfStrings objectAtIndex:indexPath.row]]; 
    return cell; 
} 

あなたは上記のサンプルを見れば、あなたは我々が続いてif文の後に我々はブランドの新しいセルまたは再利用し、セルのいずれかを持っていますそれは300にタグの設定セルにUILabelを追加していることがわかりますラベルに既にテキストが含まれています。いずれにしても、既存のラベルのテキストを行を考慮する必要があるものに変更するだけです。このようにして、何度も何度も意見を作成することを避けます。

あなたは死んでセットごUITableViewCellsあなたはこのようにそれを行うことができキャッシング上にある場合:コンソールにあなたがReceived memory warningを見たときに、デバイス上でこれを実行すると、効率的な何も驚かないとき

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row < _cells.count){ 
     return [_cells objectAtIndex:indexPath.row]; // _cells is an NSMutableArray setup in viewDidLoad 
    } 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""]; 
    cell.textLabel.text = [source objectAtIndex:indexPath.row]; // source is an NSArray of NSStrings I set up in viewDidLoad 
    [_cells addObject:cell]; 
    return cell; 
} 

&簡単なことはしばしば同じではありません。

+0

よろしくお願いいたします。それを少し修正して、ちょっと働いています。しかし、私はかなりの数のサブビューを追加するので、どのように読み込み時間を増やすかについてのアイデアはありますか?それを保存すれば、それはサブビューとリロードの節約になると思いました。思考? – iosfreak

+0

私のセルにNSLogを追加しました== nilそしてそれは8回しか出てこなかった。私がスクロールダウンしても、それはそれらを繰り返している。 – iosfreak

+0

デキュープロセスは、テーブルビューのスクロールを高速化します。あなたは、約8細胞が何度も何度も繰り返されていたと言った。それらのテーブルビューセルは一度しか作成されていないので、これは良いことです。tableviewのデキューメソッドは、新しいtableviewcellを作成するのではなく、内容を変更して再表示できるように、画面からスクロールしたtableviewcellを提供します。 – NJones

0

cell.selectionStyle,cell.backgroundColorcell.contentView.backgrounColorなどは、if (cell == nil)がtrueの場合にのみ設定されます。そのコードをifステートメントブロックの外に移動する必要があります。dequeueReusableCellWithIdentifier:がセルを生成したときと、インベントリにセルがなく、何も生成しないとき(つまり、nil)の両方で呼び出されるようにする必要があります。

関連する問題