0

テーブルビューに回答が表示されています。ユーザーがセルのいずれかを選択した場合は、テキスト側のチェックマーク付きの画像を取得し、その間に最初のセルから別のセルを選択した場合は、チェックマークを削除して、選択したセルに表示する必要があります。

4

3 に答える 3

2

まず、viewController.hファイルにプロパティを追加します

@property (nonatomic, strong) NSIndexPath *theSelectedIndexPath;

.mファイルで合成

@synthesize theSelectedIndexPath = _theSelectedIndexPath;

その後、あなたcellForRowAtIndexPath

if (indexPath.row == self.theSelectedIndexPath.row) {
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else { 
   cell.accessoryType = UITableViewCellAccessoryNone;
}   

theSelectedIndexPathを更新する ことを忘れないでくださいdidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.theSelectedIndexPath = indexPath;
}
于 2012-07-20T07:19:03.517 に答える
1

.hファイルで変数を作成します

UItableviewCell *selectedCell;

didSelectRowメソッドで、保存されたセルから選択を削除し、選択されたとおりに新しいセルを保存します。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    if (selectedCell) {
        selectedCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UItableviewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    selectedCell = cell;
}

再利用可能なセルの問題を防ぐために、uは.hファイルにUITableViewCellの代わりにNSIndexPath変数を作成できます。

NSIndexPath *selectedCellIndexPath;

そして、didSelectRowメソッドを変更します。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    [tableView scrollToRowAtIndexPath:selectedCellIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:selectedCellIndexPath];
    selectedCell.accessoryType = UITableViewCellAccessoryNone;
    UItableviewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    selectedCellIndexPath = indexPath;
}
于 2012-07-20T07:10:44.690 に答える
0
  1. グローバルintを宣言するselectedRow
  2. tableviewDidSelectRow選択した行にintを設定します。
  3. そこにもありtableviewDidSelectRowます[tableView reloadData];
  4. あなたtableView:(UITableView *)tableView cellForRowAtIndexPathの中で次のことをします:

    if (indexPath.row == selectedRow)
        cell.accessoryView = checkMark;
    else
        cell.accessoryView = nil;
    

ここで、「checkMark」はチェックマークイメージビューへのアウトレットです(私はカスタムチェックマークを使用します)

多田!

于 2012-07-20T07:17:53.500 に答える