41

iPhone の場合、複数選択できるように UITableView を構成することはできますか?

私は-setSelected:animated:各 UITableViewCell のオーバーライドを試みましたが、別のセルの選択のために UITableView が選択を解除したと考えるものから実際の選択解除を分離するのが難しいため、必要な動作をごまかすのは難しいです!

誰かが助けてくれることを願っています!

ありがとう、

ニック。

4

12 に答える 12

39

iOS5.0以降のアプリを開発している場合、次のプロパティは正常に機能するはずです

self.tableView.allowsMultipleSelection = YES;
于 2012-04-01T23:12:44.627 に答える
37

これを行う最善の方法は、選択した行ごとにチェックマークを付けることです。

選択したUITableViewCellインスタンスのaccessoryTypeをUITableViewCelAccessoryCheckmarkに設定することでそれを行うことができます。

行の選択を解除するには、UITableViewCellAccessoryNone に戻します。

どのセル/行が選択されたか (たとえば、ボタンをクリックしたとき) を列挙するには、単純にテーブルのセルを反復処理して UITableViewCellAccessoryCheckmark を探します。または、「did select」デリゲート メソッドのテーブル ビュー デリゲートで NSSet などを管理します。

于 2008-11-21T16:39:12.410 に答える
25

次のコードを使用して、セル アクセサリ タイプを設定します。

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

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}
于 2009-03-23T20:58:07.213 に答える
24

Jeff Lamarche には、これを行う方法に関するチュートリアルがあります。

http://iphonedevelopment.blogspot.com/2008/10/table-view-multi-row-edit-mode.html

私はコードを試していませんが、それが必要になる日が来ることを知って、しばらくの間頭の片隅にありました.

于 2009-03-24T05:04:51.193 に答える
11

iOS5から古いiOSにバックポートallowsMultipleSelectionDuringEditingしました。https://github.com/ud7/UDTableView-allowsMultipleSelectionallowsMultipleSelectionでフォークできます

それは置き換えのドロップであり、あなたがする必要があるのはUITableViewをUDTableViewに変更することだけです(コードまたはインターフェースビルダーで)

于 2011-12-04T05:15:02.020 に答える
5

必要なだけの複数選択の男

self.tableView.allowsMultipleSelection = YES;

viewDidLoad および

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = NO; 
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = YES;
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}
于 2012-12-11T20:31:22.503 に答える
5

HIG より:

テーブル ビューは、ユーザーがリスト アイテムを選択したときにフィードバックを提供します。具体的には、アイテムを選択できる場合、ユーザーがアイテムを選択すると、そのアイテムを含む行が一時的に強調表示され、選択が受信されたことを示します。次に、すぐにアクションが発生します。新しいビューが表示されるか、項目が選択されたことを示すチェックマークが行に表示されます。テーブル ビューでは永続的な選択状態が表示されないため、行が強調表示されたままになることはありません。

メールなどを使用するか、セルのチェックマーク アクセサリを使用して、独自の複数選択スタイルを作成する必要があります。

于 2008-11-21T12:01:30.723 に答える
3

私は同じ問題を探していました.Bhavin Chitrodaの答えは私のためにそれを解決しましたが、スクロール中にチェックマークを維持するためにいくつかの追加がありました.

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

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if ( [array indexOfObject:indexPath] == NSNotFound ) {
            [array addObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            [array removeObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

}

追加:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// Your code here
.
.
.
    if ( [array indexOfObject:indexPath] == NSNotFound ) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    return cell;

}
于 2013-01-16T10:08:57.597 に答える
2

Mail の複数選択のようなこと (たとえば、メールの削除) をしようとしている場合は、すべての選択を自分で管理する必要があるでしょう。複数行の選択は、iPhone の標準ではありません。メールは、チェックマークを使用してどの行が選択されているかを示すことでこれを解決します。

于 2008-11-21T11:46:22.797 に答える
2

行が選択されているかどうかのインジケータとしての青色で強調表示された行は、 HIGページ 121に従って実際には推奨されません。チェックマークはそのトリックを行います。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int selectedRow = indexPath.row;
    cout << "selected Row: " << selectedRow << endl;
    UITableViewCell *indexPathForCell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPathForCell.accessoryType == UITableViewCellAccessoryNone) {
        indexPathForCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        indexPathForCell.accessoryType = UITableViewCellAccessoryNone;
    }

}

次に、配列を追加するか、選択したデータを保存する方法を追加します。

于 2012-02-29T19:01:26.893 に答える
1

注: これは iOS 4 以降では機能しません。これは非公開の文書化されていない定数です。使用しないでください。

アプリを App Store に提出する予定がない場合は、UITableViewController デリゲートに次のメソッドを実装することで、複数行編集モードを呼び出すことができます。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 3; // Undocumented constant
}
于 2010-08-06T21:30:58.540 に答える
0

iOS4.3~6.0で動作確認済み

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {

    if ([controller.searchResultsTableView respondsToSelector:@selector(allowsMultipleSelectionDuringEditing)]) {
        controller.searchResultsTableView.allowsMultipleSelectionDuringEditing = YES;
    }
    else {
        controller.searchResultsTableView.allowsSelectionDuringEditing = YES;
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryCheckmark;
}
于 2012-11-12T10:52:13.320 に答える