简体   繁体   English

Swift如何从UITableViewRowAction获取访问功能

[英]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. 我是Swift的初学者,我搜索了很多东西,但找不到答案,决定在这里发布我的第一个问题。

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. 我有一个表格视图,通过使用twitter结构显示推文,并使用UITableViewRowAction向用户显示两个选项,当在行上进行滑动时,“ funnelOne”和“ funnelTwo”通过在每个推文中添加标签来对其推文进行分类。

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. 在视图控制器中,我添加了两个函数来发出警报,并获取值“ funnelTag”以将其存储到我的核心数据中。

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?? 我知道我可以在'func tableView'中编写代码以删除行,但是如何从'func tableView'中获取访问权限以成功删除行呢?

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.. 如果我实现这些功能,即使我要删除其他行也将删除第一行...第一个函数funnelTweet()正常工作,但第二个函数似乎无法正常工作。

 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. 在Second函数中,您尚未在使用索引之前对其进行初始化。

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() 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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