簡體   English   中英

為什么我看不到TableViewRowActions按鈕(快速)?

[英]Why can't I see my TableViewRowActions buttons (in swift)?

我想在表格視圖的所有單元格上執行滑動動作。 在閱讀了4個不同的教程之后,我不明白為什么我的動作按鈕沒有出現:我可以進行“滑動”,可以看到按鈕應該位於的空白區域。 甚至單擊我的處理程序也不會被調用...

class RestaurantsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var mTableView: UITableView!

private var restosList = [Restaurant]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.mTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    let url = "http://xxxxxxxxxx"
    Alamofire.request(.GET, url, parameters: ["service" : "restaurants"])
        .responseArray { (response: [Restaurant]?, error: NSError?) in
            if let response = response {
                for oneResto in response {
                    self.restosList.append(oneResto)
                }
            }
            self.mTableView.reloadData()
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.mTableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    cell.textLabel?.text = self.restosList[indexPath.row].nom

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.restosList.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var alert:UIAlertView = UIAlertView()
    alert.title = self.restosList[indexPath.row].description
    alert.addButtonWithTitle("OK")
    alert.show()

    self.mTableView.deselectRowAtIndexPath(indexPath, animated: true)
}

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

    let deleteClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        println("Delete closure called")
    }

    let moreClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        println("More closure called")
    }

    let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: deleteClosure)
    let moreAction = UITableViewRowAction(style: .Normal, title: "More", handler: moreClosure)

    return [deleteAction, moreAction]
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // Intentionally blank. Required to use UITableViewRowActions
}

將canEditRowAtIndexPath方法添加到視圖控制器,然后返回true。

我通常將處理程序代碼作為閉包形式直接放入參數中,但我認為這不會有所作為。

這是代碼:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var myItems = ["Item 1","Item 2","Item 3"]

@IBOutlet weak var myTable: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myItems.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    cell.textLabel?.text = myItems[indexPath.row]

    return cell

}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // Okie dokie
}

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


    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit", handler: {

        Void in

        println("edit swipe")

    })

    let superAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Super", handler: {

        Void in

        println("super swipe")

    })

    return [editAction, superAction]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

在此處輸入圖片說明

嘗試使用此代碼重新加載表視圖數據。

self.tableView.reloadData()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM