![](/img/trans.png)
[英]How to pass data to a UIViewController from a UITableViewRowAction in Swift?
[英]Swift How to get access func from UITableViewRowAction
我是Swift的初學者,我搜索了很多東西,但找不到答案,決定在這里發布我的第一個問題。
我有一個表格視圖,通過使用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()
}
謝謝您的幫助!
在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.