2015-01-09 58 views
13

現在、私はNSFetchedResultsControllerを使用してテーブルビューを処理しています。 スワイプ操作で削除ボタンのようなボタンを追加する方法はありますか?UITableViewスワイプに追加ボタンを追加

私はそれをサブクラス化しようと考えています。しかし、私の問題に対するヘルプ文書には何も関係がないと感じる。

ありがとうございます。

答えて

46

あなたのタイトルは間違いです。 NSFetchedResultsControllerはあなたの最終目標との関連性がありません。サブクラス化は、必要以上に多くの作業を作成するだけで、調べたいものはUITableViewRowActionです。

求められます。これは、すでに UITableViewRowActionUIAlertController

(ストレートthe docsから)簡単な概要と手を出してきた場合は、これで非常に快適に感じる必要がありますので、新しいUIAlertControllerと同様に非常に簡単に&を扱います指定された行のスワイプに応答してアクションのデリゲートを表示します。 (必須) テーブル行のいずれかにカスタムアクションを提供する場合は、このメソッドを使用します。ユーザーが横に横にスワイプすると、テーブルビューは行の内容を横に移動して操作を表示します。アクションボタンの1つをタップすると、アクションオブジェクトと共に格納されたハンドラブロックが実行されます。 このメソッドを実装していない場合、ユーザーが行をスワイプすると、標準アクセサリボタンがテーブルビューに表示されます。

カップルのノート作業を始める前に:

  • AppleはiOS8でこれを実装:ので、あなたのデプロイメント・ターゲットについて慎重に考えます。厳密にi0S8をコーディングする場合、これはサードパーティのライブラリやカスタムのUITableView編集スタイルを作成するためのすばやく簡単な代替手段です。 iOS7との互換性を保つことを目指す場合、このフォーマットではオプションが制限されます。 SIDE NOTEこれはコードに致命的なエラーを引き起こすことはありませんが、iOS7アプリはクラッシュしませんが、カスタムアクションは表示されません。
  • これの長所、すべてはあなたが最初にあなたのテーブルに呼び出すことで編集可能であることを確認しなければならないUITableViewRowActionカスタムを実装するために、関係なく、ブロック

で処理されることがあり多くのタイトルを超えてカスタマイズされていないです

  • - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
        //Obviously, if this returns no, the edit option won't even populate 
        return YES; 
    } 
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
        //Nothing gets called here if you invoke `tableView:editActionsForRowAtIndexPath:` according to Apple docs so just leave this method blank 
    } 
    

    少なくとも、移動する前に宣言する必要がある2つの方法です。

    実際に次のように一般的なガイドラインに従ってください、あなたの行のアクションボタンをカスタマイズするには:

    STEP 1あなたが見ることができるようにtableView:editActionsForRowAtIndexPath:方法

    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    } 
    

    を実装し、それはタイプにNSArrayのインスタンスメソッドですので、配列を返す必要があります。

    ステップ2配列に渡すアクションオブジェクトを追加します。例:

    { 
    
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
    { 
        // Delete something here 
    }]; 
    
    UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
    { 
        //Do whatever here : just as an example : 
        [self presentUIAlertControllerActionSheet]; 
    }]; 
    
    }  
    

    あなたはこれらの行のアクションのいくつかのプロパティを変更しますが、完全なカスタマイズオプションのための文書を参照することができます単に他のほとんどのようにそれをプリフォーム、行アクション背景色をカスタマイズするには

    をボタン内のアクション:あなたは私に配列を返す必要が前に述べたように

    delete.backgroundColor = [UIColor redColor]; 
    more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1]; //arbitrary color 
    

    STEP 3

    さらに参考のために
    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    
        UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
        { 
         // Delete something here 
        }]; 
        delete.backgroundColor = [UIColor redColor]; 
    
        UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
        { 
         //Just as an example : 
         [self presentUIAlertControllerActionSheet]; 
        }]; 
        more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1]; 
    
        return @[delete, more]; //array with all the buttons you want. 1,2,3, etc... 
    }  
    

    :あなたの完全なコードは以下のようになりますので、nstance方法LINK TO DOCUMENTATION


    EDITED FOR SWIFT SYNTAX

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 
    
    var delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
        //Do something 
    }) 
    delete.backgroundColor = UIColor.redColor() 
    
    var more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
        //Do something 
    }) 
    more.backgroundColor = UIColor.blueColor() 
    
        return [delete, more] 
    } 
    
  • +0

    ありがとう:

    は、ここでのスウィフト3版です。しかし、実際には、NSFetchedResultsControllerをカスタマイズして追加のbtnを有効にする方法を試していました。あなたの答えはこの問題を解決するための別の解決策を提供しますが、私の元の意図ではありません。もう一度あなたの答えをありがとう。 – AlexWei

    +0

    多分あなたは@AlexWeiを明確にすることができました。私は自分の答えを編集すること以上に喜んでいます。 NSFetchedResultsControllerが単にコアデータから情報を取り出してUITableViewに表示するため、私が混乱している理由があります。アクションボタンは、UITableView非フェッチコントローラの一部です。あなたの問題に参照リンクや画像を投稿することができれば、私はあなたを助けるためにもっと理解できるかもしれません – soulshined

    +1

    それは私が完全に間違っているようです。私は気づいていない "アクションボタンは、コントローラをフェッチしないUITableViewの一部です"。私はボタンがフェッチコントローラに属していると思った。 : – AlexWei

    10

    すべての栄光に@魂が受け入れられた答え。タイトルの編集についてのご提案のための

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
        let delete = UITableViewRowAction(style: .default, title: "Delete") { (action:UITableViewRowAction, indexPath:IndexPath) in 
         print("delete at:\(indexPath)") 
        } 
        delete.backgroundColor = .red 
    
        let more = UITableViewRowAction(style: .default, title: "More") { (action:UITableViewRowAction, indexPath:IndexPath) in 
         print("more at:\(indexPath)") 
        } 
        more.backgroundColor = .orange 
    
        return [delete, more] 
    } 
    
    +0

    変換をありがとう。 – RichS

    関連する問題