2011-12-05 5 views
1

これは一般的な方法ではありませんが、アプリケーションにこの機能が必要であることはわかっています。私はiPhoneプログラミングにとって非常に新しいです。ユーザーがテーブル画面をプル/スクロールすると、更新されて停止する必要があります。私が引っ張ると、データの順序も変わってきます。UITableViewでテーブルデータの読み込みをスクロールするときに停止する

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

{

static NSString *MyIdentifier = @"MyIdentifier"; 
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)]; 
    [button addTarget:self action:@selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTitle:optionValue forState:UIControlStateNormal]; 
    [button setTag:indexPath.row]; 
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 
return cell; 

}

+0

cellForRowAtIndexPathメソッドを表示してください。また、テーブルの行を更新したくない理由を教えてください。あなたが正しいことをしたら、それは問題を起こすべきではありません。 – sosborn

+0

cellForRowAtIndexPathメソッドのコードを更新しました。これらのセルに入ってくるすべてのデータはデータベースからのものです。また、ユーザーがスクロールしたときに何度もデータを再読み込みする必要はありません。データが更新されると、データの順序が変更されますが、私はそれを望んでいません。 –

答えて

-1

まず、あなたがcellForRowAtIndexPathを使用している方法が間違っています。

static NSString *MyIdentifier = @"MyIdentifier"; 
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)]; 
    [button addTarget:self action:@selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 
//These should be outside of the cell == nil loop so that they get refreshed correctly 
[button setTitle:optionValue forState:UIControlStateNormal]; 
[button setTag:indexPath.row]; 

return cell; 

あなたがoptionValueを設定する場所第二に、私は見ませんが、データベースを毎回ヒットしたくない場合は、その後、NSArrayの中に値を格納し、配列から値を取得します。この方法では、ビューをロードするときに一度データベースにヒットします。

+0

上記のoptionValueメソッドを設定するコードを削除しました。今、if(cell == nil)という行を削除すると、うまく動作します。私はNSMutable配列を使用しており、毎回データベースに感謝していません。 –

+0

私はそれを試しましたが、動作しません。つまり、セルを正しくリフレッシュしません。ありがとう。 –

+0

詳細情報を教えてください。私は助けになるかもしれません。どのように失敗するのですか? – sosborn

関連する問題