iPhone の場合、複数選択できるように UITableView を構成することはできますか?
私は-setSelected:animated:
各 UITableViewCell のオーバーライドを試みましたが、別のセルの選択のために UITableView が選択を解除したと考えるものから実際の選択解除を分離するのが難しいため、必要な動作をごまかすのは難しいです!
誰かが助けてくれることを願っています!
ありがとう、
ニック。
iPhone の場合、複数選択できるように UITableView を構成することはできますか?
私は-setSelected:animated:
各 UITableViewCell のオーバーライドを試みましたが、別のセルの選択のために UITableView が選択を解除したと考えるものから実際の選択解除を分離するのが難しいため、必要な動作をごまかすのは難しいです!
誰かが助けてくれることを願っています!
ありがとう、
ニック。
iOS5.0以降のアプリを開発している場合、次のプロパティは正常に機能するはずです
self.tableView.allowsMultipleSelection = YES;
これを行う最善の方法は、選択した行ごとにチェックマークを付けることです。
選択したUITableViewCellインスタンスのaccessoryTypeをUITableViewCelAccessoryCheckmarkに設定することでそれを行うことができます。
行の選択を解除するには、UITableViewCellAccessoryNone に戻します。
どのセル/行が選択されたか (たとえば、ボタンをクリックしたとき) を列挙するには、単純にテーブルのセルを反復処理して UITableViewCellAccessoryCheckmark を探します。または、「did select」デリゲート メソッドのテーブル ビュー デリゲートで NSSet などを管理します。
次のコードを使用して、セル アクセサリ タイプを設定します。
- (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;
}
Jeff Lamarche には、これを行う方法に関するチュートリアルがあります。
http://iphonedevelopment.blogspot.com/2008/10/table-view-multi-row-edit-mode.html
私はコードを試していませんが、それが必要になる日が来ることを知って、しばらくの間頭の片隅にありました.
iOS5から古いiOSにバックポートallowsMultipleSelectionDuringEditing
しました。https://github.com/ud7/UDTableView-allowsMultipleSelectionallowsMultipleSelection
でフォークできます
それは置き換えのドロップであり、あなたがする必要があるのはUITableViewをUDTableViewに変更することだけです(コードまたはインターフェースビルダーで)
必要なだけの複数選択の男
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;
}
HIG より:
テーブル ビューは、ユーザーがリスト アイテムを選択したときにフィードバックを提供します。具体的には、アイテムを選択できる場合、ユーザーがアイテムを選択すると、そのアイテムを含む行が一時的に強調表示され、選択が受信されたことを示します。次に、すぐにアクションが発生します。新しいビューが表示されるか、項目が選択されたことを示すチェックマークが行に表示されます。テーブル ビューでは永続的な選択状態が表示されないため、行が強調表示されたままになることはありません。
メールなどを使用するか、セルのチェックマーク アクセサリを使用して、独自の複数選択スタイルを作成する必要があります。
私は同じ問題を探していました.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;
}
Mail の複数選択のようなこと (たとえば、メールの削除) をしようとしている場合は、すべての選択を自分で管理する必要があるでしょう。複数行の選択は、iPhone の標準ではありません。メールは、チェックマークを使用してどの行が選択されているかを示すことでこれを解決します。
行が選択されているかどうかのインジケータとしての青色で強調表示された行は、 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;
}
}
次に、配列を追加するか、選択したデータを保存する方法を追加します。
注: これは iOS 4 以降では機能しません。これは非公開の文書化されていない定数です。使用しないでください。
アプリを App Store に提出する予定がない場合は、UITableViewController デリゲートに次のメソッドを実装することで、複数行編集モードを呼び出すことができます。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return 3; // Undocumented constant
}
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;
}