2015-08-14 53 views
0

我是Swift的初学者,我搜索了很多东西,但无法弄清楚并决定在此发布我的第一个问题。Swift如何从UITableViewRowAction获取访问func

我有一个表格视图,通过使用twitter结构来显示推文,我使用UITableViewRowAction向用户展示了两个选项:“funnelOne”和“funnelTwo”,通过添加标签对他们的推文进行分类到每条推文。

在视图控制器中,我添加了两个函数来生成警报并获取'funnelTag'的值以将其存储到我的核心数据中。

但是,我不确定是否可以将数字正确存储到核心数据,因为如果我按下其中一个可滑动按钮,将会删除不同的单元格。我知道我可以在'func tableView'中编写代码来删除该行,但是如何从'func tableView'中获取访问权限以成功删除该行?

如果可以解决,我应该能够成功地将值存储到我的核心数据。

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) 

    } 

这些是我添加的两个功能。如果我实现这些功能,即使我想要删除其他行,第一行也会被删除...第一个函数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() 
    } 

谢谢你的帮助!

回答

1

在第二个函数中,您在使用它之前没有初始化索引。

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

谢谢。它有助于!! – EastCool

+0

请给我一个投票或正确的答案,因为我在这里是一个初学者,并需要一些特权,在此先感谢。 –