6

の次の行を使用してUITableView、編集モードで複数選択用に自動的に設定される がありますviewDidLoad

self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self setEditing:YES animated:YES];

ただし、各行の左側に自動的に表示されるチェックマークではなく、背景色を変更して行が選択されたことを示したいと思います。(たとえば、メールアプリでメーリングリストを編集するときに表示されるもの、またはこのSOの質問で議論されているもの。)それらのチェックボックスを取得できないことを除いて、ほとんどの部分で機能しています。UITableViewを編集モードにする一部として自動的に作成され、削除されます。

以下は私が取り組んでいるコードです:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _Hierachy.cellCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

これらはUITableView私がこれまでに得た唯一の方法なので、他のすべてはデフォルトの動作でなければなりません。

編集モードで、左側にあるチェックマークを非表示にする方法を知っている人はいますか? セルのアクセサリ部分のチェック マークについて多くの質問を見てきましたが、私が理解しているように、これは別のものです。メソッドについて話している人も見ましtableView:didSelectRowAtIndexPath:たが、これらのチェックマークは、テーブルが編集モードになると作成され、ユーザーが [完了] をタップすると消えるので、そのメソッドは関係ないようです。

私が最も近いのは、この方法を見つけることです:

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

しかし、それはセルのコンテンツがインデントされてチェックマークのスペースを空けることを妨げているだけです。チェックマークは引き続き表示されます。

これらのチェックマークを非表示にして、編集モードで複数選択を許可する方法はありますか? それとも、これらのチェックマークはUITableView、複数選択が有効になっている編集モードでは必須の動作ですか?

編集:私は(しぶしぶ)、チェックマークのフレームが画面から消えるまで移動するなど、ややハックな回答を受け入れます。このアプリは内部使用のためのものであり、App Store で承認される必要はありません。しかし、UITableView編集モードに移行するとチェックマークが自動的に作成されるため、変更するオブジェクトとしてチェックマークを取得する方法さえわかりません。どんな助けでも大歓迎です!

4

3 に答える 3

10

(void)setEditing:animated:UITableViewCell をサブクラス化し、次のようにメソッドをオーバーライドする必要があります。

#import "MyCustomCell.h"

@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView
{
    //Cell Selected Color: CLEAR
    [super setSelectedBackgroundView:selectedBackgroundView];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //Cell Edit Mode NO Indent & Selected Color: CLEAR
    [super setEditing:NO animated:animated];
    [self setNeedsLayout];
}

@end

それを行った後、に移動してInteface Builder、セルをクラスの一部にしますMyCustomCell

IB で MyCustomCell クラスのセル部分を作成したら、コードにインポートMyCustomCell.hしてUITableViewController以下を変更します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

更新: TableView で次のことを行うこともできますtableView:editingStyleForRowAtIndexPath:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
            return UITableViewCellEditingStyleNone;
}

ただし、セルがインデントされます。そのインデントを削除するには、Cell をサブクラス化する必要があります。

これを行うと、準備が整います。私はちょうどそれをテストしました、そしてそれはあなたが望むように動作します!

于 2014-10-03T18:28:52.763 に答える
1

チェックマークのない複数選択の最も簡単な解決策は次のとおりです。

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
    return NO;
}

これにより、デフォルトの選択スタイル (グレーまたはカスタム選択背景を使用) を使用してセルが選択され、チェックマークは表示されません。


あなたが選んだ解決策について一言。ユーザーは複数のアプリケーションで一貫したエクスペリエンスを期待しており、これらのチェックマークはこの一貫性の一部です。通常の OS のルック アンド フィールを変更するには、十分な理由があることを確認してください。

于 2014-10-03T18:42:07.337 に答える
0

達成する方法は次のとおりです。

  1. スワイプで作品を削除
  2. 編集モードでは、セルの左側にチェックボックス、アイテムの削除、またはその他のものが表示されません
  3. セルのインデントは引き続き正常に機能します (必要に応じてこれをオフにすることができます)。
  4. (ボーナス) 複数選択をサポート

そう - あなたの UITableViewDelegate で

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isEditing)
    {
      return UITableViewCellEditingStyleNone;
    }
    else
    {
      return UITableViewCellEditingStyleDelete;
    } 
}

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
  (do stuff)
}

UITableView も構成する必要があります

self.table.allowsMultipleSelectionDuringEditing=NO;

実際に複数選択必要な場合;

self.table.allowsSelectionDuringEditing=YES;

次に、選択したセルを自分で管理します。

UITableViewCell サブクラスにカスタム チェックボックスを配置し、それに応じて値も変更します。

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

質問者は、背景色で選択を示したいと考えています。その部分は単純明快です。

于 2015-11-27T18:54:15.383 に答える