【问题标题】:IOS 8 UItableView Swipe To deleteIOS 8 UItableView 滑动删除
【发布时间】:2014-12-16 16:55:52
【问题描述】:

我有 UIviewController 包含一个 UITableView ,这是一个非常简单的代码,我只想滑动一个单元格并显示更多内容并删除和处理这两个事件,这是我非常简单的类

class ViewController: UIViewController  , UITableViewDataSource , UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

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

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



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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let identifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "\(indexPath.row)"

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
{
    var moreAction = UITableViewRowAction(style: .Default , title: "More") { (action : UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        println("more")
    }
    moreAction.backgroundColor = UIColor.lightGrayColor()

    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action: UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        println("delete")
    }

    return [moreAction , deleteAction]
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    if (editingStyle == .Delete)
    {
        println("delete1")

    }
    if(editingStyle == .Insert)
    {
        println("insert")

    }

}

editActionsForRowAtIndexPath 函数被调用,但没有 RowActions 出现! 有什么问题 ? ,

【问题讨论】:

    标签: uitableview ios8 delete-row swipe-gesture


    【解决方案1】:

    您需要在自定义按钮的处理程序中做一些事情。

    例如,使用自定义删除按钮(并且只是为了更改其标签和背景颜色),您可以执行以下操作:

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        let button = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: NSLocalizedString("TitleOfDeleteButton", comment: "Delete"), handler: { (rowAction, indexPath) -> Void in
            self.tableView(self.tableView!, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath)
        })
        button.backgroundColor = UIColor.lightGrayColor()
        return [button]
    }
    

    另外,别忘了UITableViewDelegate协议的这种方法只有iOS8自带

    【讨论】:

      猜你喜欢
      • 2014-11-18
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多