21

このコードを使用すると、テーブル内の複数の行を確認できます。

しかし、私が望むのは、一度に 1 つの行だけをチェックすることです。これが私のコードです:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (theCell.accessoryType == UITableViewCellAccessoryNone) {
        theCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        theCell.accessoryType = UITableViewCellAccessoryNone;
    }
}

ユーザーが別の行を選択した場合、古い行のチェックを自動的に解除するだけです。それを行う方法がわかりません。

4

18 に答える 18

27

didSelectRowAtIndexで選択されたインデックスを追跡するための新しい変数を作成できます。

int selectedIndex;

あなたのcellForRowAtIndexPathで

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

そしてあなたのdidSelectRowAtIndexで

selectedIndex = indexPath.row;
[tableView reloadData];
于 2013-03-15T07:18:37.010 に答える
26

最善の方法は、アクセサリ タイプを に設定することcellForRowAtIndexPathです。

didSelectRowAtIndexPathどのパスを選択する必要があるかを記録するためにのみ使用してから、 を呼び出しますreloadData

于 2012-04-17T14:22:43.603 に答える
23

選択するたびにテーブルをリロードする必要はありません (すべきではありません)。

Apple には、選択リストの管理方法に関する適切なドキュメントがあります。例については、コード リスト 6-3 を参照してください。

これは、他のいくつかの回答とほぼ同じ回答ですが、もう少し詳細を追加すると思いました。

あなたがしたいことは、現在選択されているIndexPathを変数に保存し、それをdidSelectRowAtIndexPathで使用して古いチェックマークを削除することです。これは、新しいチェック マークを追加する場所でもあります。

cellForRowAtIndexPath のチェックマークも設定/設定解除する必要があります。そうしないと、リストが大きく、セルが再利用される場合、複数の行が選択されているように見えます。これは、他のいくつかの回答の問題です。

以下のSwift 2.0の例を参照してください。

// for saving currently seletcted index path
var selectedIndexPath: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0)  // you wouldn't normally initialize it here, this is just so this code snip works
// likely you would set this during cellForRowAtIndexPath when you dequeue the cell that matches a saved user selection or the default

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // this gets rid of the grey row selection.  You can add the delegate didDeselectRowAtIndexPath if you want something to happen on deselection
    tableView.deselectRowAtIndexPath(indexPath, animated: true) // animated to true makes the grey fade out, set to false for it to immediately go away

    // if they are selecting the same row again, there is nothing to do, just keep it checked
    if indexPath == selectedIndexPath {
        return
    }

    // toggle old one off and the new one on
    let newCell = tableView.cellForRowAtIndexPath(indexPath)
    if newCell?.accessoryType == UITableViewCellAccessoryType.None {
        newCell?.accessoryType = UITableViewCellAccessoryType.Checkmark
    }
    let oldCell = tableView.cellForRowAtIndexPath(selectedIndexPath!)
    if oldCell?.accessoryType == UITableViewCellAccessoryType.Checkmark {
        oldCell?.accessoryType = UITableViewCellAccessoryType.None
    }

    selectedIndexPath = indexPath  // save the selected index path

    // do whatever else you need to do upon a new selection
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    // if this is the currently selected indexPath, set the checkmark, otherwise remove it
    if indexPath == selectedIndexPath {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
    } else {
        cell.accessoryType = UITableViewCellAccessoryType.None
    }
}
于 2015-12-10T04:20:51.163 に答える
8

tableView をリロードする必要はありません。

以下のコードを参照してください。

クラスの NSIndexPathlastSelectedIndexPath変数を宣言する

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(lastSelectedIndexPath) {

        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelectedIndexPath];
        [lastCell setAccessoryView:nil];
    }

    UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:currentIndexPath];
    [currentCell setAccessoryView:UITableViewCellAccessoryCheckmark];

    lastSelectedIndexPath = indexPath;
}
于 2013-07-09T04:48:46.147 に答える
4

最も簡単な方法は、選択した IndexPath を保存してチェックインすることですcellForRowAtIndexPath

  1. 選択したインデックス パスを初期化します。

    selectedIndexPath = [[NSIndexPath alloc] init];
    
  2. cellForRowAtIndexPath

    if([selectedIndexPath isEqual:indexPath]){
        [checkMark setHidden:NO];
    } else {
        [checkMark setHidden:YES];
    }
    
  3. didSelectRowAtIndexPath

    selectedIndexPath = indexPath;
    [tableView reloadData];
    

このNjoyを試してみてください:)

于 2013-06-12T11:56:10.283 に答える
4

https://stackoverflow.com/a/5960016/928599が役立つと思います。
私はそれを使用しましたが、deselectRowAtIndexPath でも動作します!

于 2012-06-07T09:26:12.153 に答える
1

Swift では、以下は完全に機能します。

lastSelectedIndexPath = NSIndexPath(forRow: 1, inSection: 0)//Row will be the previous selected row


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell:LabelsSelectionCell = tableView.dequeueReusableCellWithIdentifier("LabelsSelectionCell", forIndexPath: indexPath) as! LabelsSelectionCell
    cell.setCellLael(labelOptionsArray[indexPath.row])
    if lastSelectedIndexPath == indexPath
    {
        cell.setCellCheckedStatus(true)
    }
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    if let _ = lastSelectedIndexPath
    {
        let lastCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath!) as! LabelsSelectionCell
        lastCell.setCellCheckedStatus(false)
    }
    let currentCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(indexPath) as! LabelsSelectionCell
    currentCell.setCellCheckedStatus(true)

    lastSelectedIndexPath = indexPath

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
于 2016-03-24T12:37:37.837 に答える
1

これを試して:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 1 {
        // un-select the older row
        if let selected = self.LastSelected {
            tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None
        }

        // select the new row
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
        self.LastSelected = indexPath
    }
}
于 2015-05-15T02:32:23.590 に答える
0

スウィフト iOS

 var checkedIndexPath : NSIndexPath?

// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    
    println("Section #\(indexPath.section)! You selected cell #\(indexPath.row)!")
    
    if (self.checkedIndexPath != nil) {
        var cell = tableView.cellForRowAtIndexPath(self.checkedIndexPath!)
        cell?.accessoryType = .None
    }
    
    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.accessoryType = .Checkmark
    
    self.checkedIndexPath = indexPath
    
}// end tableView
于 2015-02-24T13:52:25.047 に答える
0

Swift 2.0では、これを使用しました:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if((lastSelectedIndexPath) != nil) {
        let lastCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
        lastCell?.accessoryType = UITableViewCellAccessoryType.None
    }

    let currentCell = tableView.cellForRowAtIndexPath(indexPath)
    currentCell?.accessoryType = UITableViewCellAccessoryType.Checkmark

    lastSelectedIndexPath = indexPath

}
于 2016-02-17T19:57:18.850 に答える
0

私のやり方は、選択中に他のセルの選択を解除することです:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = ....

    if condition {
        cell.accessoryType = .checkmark
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.bottom)
    } else {
        cell.accessoryType = .none
    }

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    for row in 0...tableView.numberOfRows(inSection: 0) {
        if row == indexPath.row {continue}
        tableView.deselectRow(at: IndexPath(row: row, section: 0), animated: true)
    }

    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = .none
}
于 2016-12-20T09:13:32.633 に答える
0

1行のコードでカスタムセルタイプでそれを行うことができます.

final class CheckableTableViewCell: UITableViewCell {

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    self.accessoryType = selected ? .checkmark : .none
  }

}

このメソッドを機能させるには、セルを選択する必要があります。

選択したセルのハイライトを表示したくない場合は、ストーリーボードまたはコードでセルの selectionStyle を .none に設定するだけです

あなたが呼び出す場合、このメソッドは機能しません

tableView.deselectRow(at: indexPath, animated: true)

複数選択でもうまく機能します。

于 2019-10-18T13:23:48.930 に答える
0

テスト済みで解決済みこれを試してみてください。完全に機能しました

これをグローバル変数として追加します

var selectedIndexPath = NSIndexPath(row: 0, section: 0)

これを didselect メソッドに追加します

        // old one check off
        if indexPath != selectedIndexPath as IndexPath {
            let oldCell = categorytable.cellForRow(at: selectedIndexPath as IndexPath)
            if oldCell?.accessoryView != nil {
                oldCell?.accessoryView = nil
            }
            else {
                let imageName = "checkmark"
                let image: UIImageView = UIImageView(image: UIImage(named: imageName))
                cell.accessoryView = image
            }
        }
        // the new one on

        if  cell.accessoryView == nil {
            let imageName = "checkmark"
            let image: UIImageView = UIImageView(image: UIImage(named: imageName))
            cell.accessoryView = image
        }
        else {
            cell.accessoryView = nil
        }
于 2017-08-17T07:58:53.550 に答える