0

TableviewCellチェックボックスを含むカスタムを使用しているアプリケーションを作成しているのでimageview、ユーザーが行を選択した場合にチェックされていないイメージをその上に配置し、チェックされているイメージを変更したいので、このイメージをDidSelectRowAtIndexPathメソッドで変更するにはどうすればよいですか?

私はこれを試しましたが、うまくいきません

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *checkBoxIdentifier = @"checkBox";
    UITableViewCell *cell ;
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBox" owner:self options:nil];
    cell = [nib objectAtIndex:0];
    cell =  [tableView cellForRowAtIndexPath:indexPath];    
    cell.checkBoxImageView.image = [UIImage imageNamed:@"uncheckedimage"];
}
4

1 に答える 1

4

これを行うだけで、他に何も必要ありません。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //replace 'CustomCell' with your custom cell class name
    CustomCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    cell.checkBoxImageView.image = [UIImage imageNamed:@"uncheckedimage"];
}

編集: ただし、テーブルビューがリロードされると、セルが選択されていません。そのために、カスタム セル クラス ヘッダー ファイルで BOOL プロパティを作成します。

@property (retain) BOOL isSelected;

didSelectRowAtIndexPath を次のように変更します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //replace 'CustomCell' with your custom cell class name
    CustomCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    cell.checkBoxImageView.isSelected = YES;
    [tableView reloadData];
}

すでにチェックされている行の選択を解除する場合:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    //replace 'CustomCell' with your custom cell class name
    CustomCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    cell.checkBoxImageView.isSelected = NO;
    [tableView reloadData];
}

そしてあなたの cellForRowAtIndexPath メソッドで:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     /*
      initialize and set cell properties like you are already doing
     */
     if(cell.isSelected) {
          cell.checkBoxImageView.image = [UIImage imageNamed:@"uncheckedimage"];
     }
     else {
          cell.checkBoxImageView.image = [UIImage imageNamed:@"checkedimage"];
     }
}
于 2013-10-23T05:35:27.320 に答える