Using NSTableView Animations with Bindings

前端 未结 5 1052
滥情空心
滥情空心 2020-12-25 08:26

I have a NSTableView that is bound to a NSArrayController. The NSArrayController\'s contentSet property is bound to a

5条回答
  •  有刺的猬
    2020-12-25 08:56

    Found myself in this same situation: wanted to use bindings as much as possible (minimize amount of glue code) and still be able to add small pieces of logic specific to my app.

    I have an NSTableView that exposes a delete button on every one of its rows. The delete button is hooked up to an IBAction on my NSViewController subclass. The table is properly bound to an NSArrayController (done in my Storyboard via IB). I also wanted an animation on row deletion.

    I'm using swift (but I think it should be pretty straightforward to translate this to objective-c). The only way I got this to work with bindings, was to use a timer to deferred the deletion of the object from the NSArrayController (using half a second delay below - change it to suit your needs):

    import Cocoa
    
    class ProjectsController: NSViewController {
    
        @IBOutlet var arrayController: NSArrayController!
        @IBOutlet weak var tableView: NSTableView!
    
        @IBAction func deleteRow( object: AnyObject ) {
            let row = tableView.rowForView( object as! NSView )
            if ( row > -1 ) {
                let indexSet = NSIndexSet( index:row )
                tableView.removeRowsAtIndexes( indexSet, withAnimation: NSTableViewAnimationOptions.EffectFade )
                NSTimer.scheduledTimerWithTimeInterval( 0.5, target: self, selector: "rowDeleted:", userInfo: row, repeats: false )
            }
        }
    
        func rowDeleted( timer:NSTimer ) {
            let row = timer.userInfo as! Int
            arrayController.removeObjectAtArrangedObjectIndex( row )
        }
    }
    

提交回复
热议问题