简体   繁体   中英

Swift How to get access func from UITableViewRowAction

I'm a beginner of Swift, and I searched a lot of stuffs, but couldn't figure out and decided to post my first question ever here.

I have a table view to show tweets by using twitter fabric and I use UITableViewRowAction to present two options to users when a swipe is done on a row, "funnelOne" and "funnelTwo", to categorize their tweets by adding tags to each tweet.

In the view controller, I add two functions to make an alert and get a value of 'funnelTag' to store it to my core data.

However, I am not sure if I can correctly store the number to the core data because somehow different cell would be deleted if I push one of swipeable buttons. I know I can write a code within 'func tableView' to delete the row, but how I can get access from out of 'func tableView' to delete the row successfully??

If it can be resolved, I should be able to successfully store the value to my core data.

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath) as? CustomTableViewCell

    let funnelOne = UITableViewRowAction(style: .Default, title: "funnel") {
        (action, indexPath) -> Void  in
         funnelTag = 1  // funnelTag is Int I want to store in my Core Data
         cell!.tag = funnelTag

         // self.tweets.removeAtIndex(indexPath.row) - I know this is working
        tableView.setEditing(false, animated: true)
        }

    let funnelTwo = UITableViewRowAction(style: .Default, title: "funnel") {
        (action, indexPath) -> Void in
        funnelTag = 2
        cell!.tag = funnelTag
        tableView.setEditing(false, animated: true)
        // self.tweets.removeAtIndex(indexPath.row)

    }

These are two functions I add. if I implement these functions, top row would be deleted even though I want to delete other row... the first function, funnelTweet() is properly working, but the second function does not seem to work correctly..

 func funnelTweet(cell: CustomTableViewCell){
    let index: Int = cell.tag
    if SettingStore.sharedInstance.isNoAlert(){
        self.submitFunnel(index, cell: cell)
    } else {
        self.alert = UIAlertController(title: NSLocalizedString("stock_confirm_funnel", comment: ""), message: nil, preferredStyle: .Alert)
        self.alert!.addAction(UIAlertAction(title:  NSLocalizedString("common_ok", comment: ""), style: .Destructive) { action in
            self.submitFunnel(index, cell: cell)
            })
        self.alert!.addAction(UIAlertAction(title: NSLocalizedString("common_cancel", comment: ""), style: .Cancel) { action in
            cell.moveToLeft()
            })
        self.presentViewController(self.alert!, animated: true, completion: nil)
    }
}

func submitFunnel(index: Int, cell: CustomTableViewCell){ 
    var tweet = self.tweets[index]
        // store to local storage
        TagStore.sharedInstance.saveData(tweet.createdAt, funnelTag: funnelTag, id: tweet.tweetID)
        self.tweets.removeAtIndex(index)
        self.tableView!.reloadData() 
    }

Thank you for your help!

In the Second function you have not initialized the index before using it.

func submitFunnel(index: Int, cell: CustomTableViewCell){ 
// Initialize index here before using it in the next statement.. that is give it a value, otherwise it will return nil
 var tweet = self.tweets[index]
    // store to local storage
    TagStore.sharedInstance.saveData(tweet.createdAt, funnelTag: funnelTag, id: tweet.tweetID)
    self.tweets.removeAtIndex(index)
    self.tableView!.reloadData() 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM