2015-01-12 5 views
11

カスタムスライドアウトボタンを追加したUITableViewがあります。これらはすべて期待通りに機能します。しかし、私のテーブルには、スライドアウトボタンのどれも関連性のない状態に行があり、スライドアウトボタンが表示されないように空の配列のアクションを戻しているシナリオがあります。残念なことに、これが起こると、UITableViewはeditActionsForRowAtIndexPathの呼び出しを中止して、テーブル内のすべての行のスライドアウトボタンを無効にします。アプリが再起動されるまで永続的に見えます。空の配列を返すと、UITableView editActionsForRowAtIndexPathが機能しなくなる

これが期待どおりの動作ですか?

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? 
{ 
    if mydata[indexPath.row].EditAvailable() 
    { 
     var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler) 
     return [editAction] 
    } 
    else 
    { 
     return [] 
    } 
} 
+1

A [ロングショット](http://nshipster.com/at-compiler-directives/)が、[] –

+0

私はObjective-Cのから@記号を認識し...しかし、それは@リターンを試してみてください迅速に有効ですか? –

+0

私はそれが素早くここのどこかで使用されているのを見たと思っていましたが、(配列の作成と初期化のために)今見つけられません。 mydataは常にmydata.length == indexpath行数でアクセス可能な配列ですか? –

答えて

0

私はしばらく前に(迅速にテストしていない)OB-Cを使用して、いくつかのテーブルセルに対するアクションの同様の選択的な使用を実現しますが、私は最後の1を除くすべてのアクションを持っているの下に、handleSwipeでselectivenessを置きます:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{ 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location]; 
    if(!(swipedIndexPath.row<[coreDataHelper getAllTimerLists].count)){ 
     [self.tableView setEditing:!self.tableView.editing animated:YES]; 
    } 
} 

これは、1つの配列しか持たず、それを非アクティブ化/アクティブ化するのではなく、複数の異なるアクション配列を必要としますか?

+0

これは興味深い別の提案です。私はそれを正しく理解している場合は、スワイプのイベントを傍受してからtableviewに到達する前に、editActionsForRowAtIndexPathが呼び出されて空のアクション配列が返されるのを防ぎ、動作を回避します。 –

+0

それは正しいです。 (これは本番環境で使用されています)。これのためには –

7

tableView:editingStyleForRowAtIndexPath:を試してください。
スライド自体のオン/オフを制御できます。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (/* mydata[indexPath.row].EditAvailable() */) { 
     return UITableViewCellEditingStyleDelete; 
    } else { 
     return UITableViewCellEditingStyleNone; 
    } 
} 

(私はObjective-Cでテストした)

20

私はこの問題を解決方法がここ

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 

、機能を実装することでしたあなたがいない場合、あなただけのfalseを返す必要があります予想される行にスワイプ機能が必要です。

だからコードは、この

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return mydata[indexPath.row].EditAvailable() 
} 

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? 
{ 
    var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler) 
    return [editAction] 
} 

editActionsForRowAtIndexPathは、その後、あなただけは指示された編集可能なもののために呼ばれるようになります。

1

editActionsForRowAtIndexPathのアクションで空の配列を返さないでください。行を編集できないようにしたい場合... canEditRowAtIndexPathでfalseを返します。空の配列を返すと、すべての行がスワイプボタンの表示を停止します。その奇妙な振る舞い、私はそれが不具合かどうかわからない。

3

最初に、editActionsForRowAtIndexPathは、アクションがないときは[]でなくnilを返す必要があります。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? 
{ 
    if mydata[indexPath.row].EditAvailable() 
    { 
     var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler) 
     return [editAction] 
    } 
    else 
    { 
     return nil 
    } 
} 

第2に、editingStyleForRowAtIndexPathを実装して、他の行のデフォルトの「削除」アクションを無効にする必要があります。

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 
    if mydata[indexPath.row].EditAvailable() { 
     return UITableViewCellEditingStyle.Delete 
    } 
    return UITableViewCellEditingStyle.None 
} 

先端空の配列/ nilの先端をありがとうlucianoはで空の配列を渡す

6

避ける -

-

のtableView editActionsForRowAtIndexPath

はより良い状態のチェックを行います

のtableView canEditRowAtIndexPath

フォーマットこのようなあなたのコード:iOS9.3で

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     if mydata[indexPath.row].EditAvailable() { 
      return true 
     } 
     return false 
    } 

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? 
    { 
     var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler) 
     return [editAction] 
    } 
+1

+1。すばらしいです。私のアプリは 'tableView editActionsForRowAtIndexPath'の中に空の配列を渡すとクラッシュします。 ** edit **とは既に編集可能なものがあるはずなので、意味があると思います。再度、感謝します。 – Ron

2

は、 "canEditRowAtIndexPath" は動作しません!

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

したがって、次のコードが役に立ちます。 2つの方法のみを実装する必要があります。

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(//not editable){ 
     return nil; 
    } 
    return @[//your actions]; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if(//not editable){ 
     return UITableViewCellEditingStyleNone; 
    } 
    return UITableViewCellEditingStyleDelete; 
} 
+0

これも気付きました、アップルはこれをやっていますか? – andromedainiative