gpt4 book ai didi

ios - Swift 如何从 UITableViewRowAction 获取访问功能

转载 作者:行者123 更新时间:2023-11-29 01:45:10 28 4
gpt4 key购买 nike

我是 Swift 的初学者,我搜索了很多东西,但无法弄清楚,决定在这里发布我的第一个问题。

我有一个表格 View ,可以使用 Twitter Fabric 显示推文,并且当在一行上完成滑动时,我使用 UITableViewRowAction 向用户呈现两个选项“funnelOne”和“funnelTwo”,通过添加标签来对推文进行分类每条推文。

在 View Controller 中,我添加了两个函数来发出警报并获取“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()
}

感谢您的帮助!

最佳答案

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

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

关于ios - Swift 如何从 UITableViewRowAction 获取访问功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32015998/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com