在swift2中滑动tableviewcells

时间:2016-01-05 04:50:53

标签: ios iphone swift2

我们如何在swift2中执行向左滑动和向右滑动

    foreach($product as $key=>$p){
    $data = array(
    'product'=>$p,
    'sub_product'=>$sub_product[$key],
    'plan'=>$plan[$key],
    'months'=>$months[$key]
    );

    //Code to insert this array to Database
    // Create connection
         $conn = mysqli_connect($servername, $username, $password, $dbname);
         $sql = "INSERT INTO `table`('product','sub_product',...) VALUES ($data['product'],$data['sub_product'],...)";
         mysqli_close($conn);

    //OR If you are using Codeigniter,
         $this->db->insert('table',$data);

    }

2 个答案:

答案 0 :(得分:4)

仅提供可用于帮助您从右向左滑动的功能

以下是它的示例:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
        println("more button tapped")
    }
    more.backgroundColor = UIColor.lightGrayColor()

    let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
        println("favorite button tapped")
    }
    favorite.backgroundColor = UIColor.orangeColor()

    let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
        println("share button tapped")
    }
    share.backgroundColor = UIColor.blueColor()

    return [share, favorite, more]
}

然后它看起来像:

enter image description here

如果您想从左向右滑动,您需要自己编码,或者您可以在此处使用此库:

https://github.com/CEWendel/SWTableViewCell

答案 1 :(得分:0)

只需一种方法即可在表格视图中启用滑动删除:tableView(_:commitEditingStyle:forRowAtIndexPath:)。当用户尝试使用滑动删除其中一个表行时,会调用此方法,但它的存在是首先允许滑动删除的内容 - 也就是说,iOS会逐字检查该方法是否存在,并且,如果是,则启用滑动删除。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        objects.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}
相关问题