1

テーブルビューのすべてのセルに「blank_starアイコン」を作成しようとしています。ユーザーがクリックすると、星は「塗りつぶされた」星になります。

UIButton以下のようなサブクラスを作成しました

//.h file
@interface Bleh : UIButton {

}

+(id)specialInit;
-(void)vvv;

@end

//.m file
@implementation Bleh

+(id) specialInit
{
    Bleh* button=[super buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"blank_star.png"] forState:UIControlStateNormal];    
    [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateDisabled];    
    [button addTarget:button action:@selector(vvv)    forControlEvents:UIControlEventTouchUpInside];    
    NSLog(@"%d",[button isEnabled]);
    return button;
}


-(void)vvv
{
    NSLog(@"button tapped");
    [self setEnabled:false];
}

@end

cellforRow:次のように、テーブルビューのメソッドにUIButtonのサブクラスを追加しました。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    int row = indexPath.row;
    NSString *cc = [array objectAtIndex:row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        button.frame = CGRectMake(0, 0, 100, 100);
        NSLog(@"Button:%@ at row number: %i",button, indexPath.row);

        cell.textLabel.text = cc;
        [cell.contentView addSubview:button];

    }
    return cell;
}

ただし、アプリの実行時に問題が発生します。たとえば、「a」とマークされたセルをクリックすると、期待どおりに星が塗りつぶされます。

ここに画像の説明を入力してください

奇妙なことに、下にスクロールすると、星が点灯している他のセルも表示されます(セル「e」を参照)。

ここに画像の説明を入力してください

誰かがこれが起こっている理由を説明するのを手伝ってもらえますか?stateセルのが他のセルで再利用されているようです。どうすればこれを回避できますか?

4

2 に答える 2

1

セルは再利用されます。私もかつてこの問題を抱えていました。これは、古いセルをメモリから削除しないため、メモリを少し集中的に使用する可能性がありますが、コーディングが簡単になります。1つのトリックはこれを行うことです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    NSString *CellIdentifier = [NSString stringWithFormat@"Cell %i", row];
    NSString *cc = [array objectAtIndex:row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        button.frame = CGRectMake(0, 0, 100, 100);
        NSLog(@"Button:%@ at row number: %i",button, indexPath.row);

        cell.textLabel.text = cc;
        [cell.contentView addSubview:button];

    }
于 2011-06-06T04:36:15.013 に答える
1

ボタンの状態を NSMutableArray に格納し、セルを描画するときに、NSMutableArray に基づいて有効または無効に設定できます。配列の値を変更するには、セルにタグを付けて vvv セレクターを変更する必要があります。

//.h file
@interface Bleh : UIButton {
    NSMutableArray *data;
}

あなたの機能について

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    // Configure the cell...    
    Bleh *button = [Bleh specialInit];
    [button setTag:row] // row is the id of the button
    if([data objectAtIndex:row] isEqualToString:@"ENABLED"]) [button setEnabled:TRUE];
    else [button setEnabled:FALSE];
    ...
}

vvv セレクターで

-(void)vvv:(id)sender {
    if([sender isEnabled]) {
        [sender setEnabled:FALSE];
        [data replaceObjectAtIndex:[sender tag] withObject:@"DISABLED"];
    }
    else {
        [sender setEnabled:TRUE];
        [data replaceObjectAtIndex:[sender tag] withObject:@"ENABLED"];

    }
}

そして、viewDidLoadで配列を初期化する必要があります.10個のセルとしましょう

- (void)viewDidLoad
{
    ...
    data = [[NSMutableArray alloc] initWithCapacity:10];
    for ( int i = 0; i < 10; i++ ) {
        [data addObject:@"ENABLED"];
    }
    ...
}
于 2011-06-06T05:08:20.073 に答える