Swift语言IOS8开发战记7.Delete TableViewCell

之前演示了Alert和ActionSheet的用法,如果我们不想要某一行cell了,那么就需要删除选项。首先来体验一下Swift内置的删除方法。也是一个tableView的代理方法。

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

我们划动屏幕划出来的按钮显示的是“editingStyle”的值,默认的是“Delete”,效果如图


首先我们在删除这个代理方法中需要改变tableView,而之前的代码中tableView是在viewDidLoad()中定义的,所以首先要把tableView变成全局变量。

var tableView = UITableView()

之后回到删除功能的代理方法中,增加如下代码:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            self.restaurantImages.removeAtIndex(indexPath.row)
            self.restaurantNames.removeAtIndex(indexPath.row)
            self.restIsMark.removeAtIndex(indexPath.row)
        }
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
    }

我们首先要把每个cell所显示的内容清空,然后再把这一行删掉,比如我看到名为cg3的吃雪糕的表情很嘲讽,那么往左划点击Delete,好了,永远看不到他了。注意deleteRowsAtIndesPaths方法的第一个参数要求是数组类型,所以写成了[indexPath]


如果我们并不满足于只有一个Delete的功能,想要在和Delete并列着来点别的按钮,比如我觉得哪个小人很喜欢,我想把他加为好友,Swift也提供了这样的功能,但是在IOS8之前的OC语言中是没有提供的。同样添加一个代理方法如下:

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

这个方法返可以允许我们提供多个按钮,返回的是个数组类型。完善代码如下:

 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        let shareAction = UITableViewRowAction(style: .Default, title: "Share", handler: {
            (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
                   })
    
        let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: {
            (action: UITableViewRowAction!,indexPath: NSIndexPath!) -> Void in
        })
return [deleteAction, shareAction]
    }

运行效果如图:


然后我们给刚添加的按钮中添加功能,向share中添加一个ActionSheet试试,代码如下:

let shareAction = UITableViewRowAction(style: .Default, title: "Share", handler: {
            (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
            let menu = UIAlertController(title: "Share Action", message: nil, preferredStyle: .ActionSheet)
            let  cancel = UIAlertAction(title:"Cancel", style: .Cancel, handler: nil)
            self.presentViewController(menu, animated: true, completion: nil)
                   })

运行效果如图:



现在要实现分享功能,继续向shareAction中添加代码

let csdnAction = UIAlertAction(title: "csdn", style: .Default, handler: nil)
            menu.addAction(csdnAction)

使我们可以分享到csdn,如图:



如果要真的实现分享功能,我们需要在handler中添加代码.至于之前的删除行的功能,代码在

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 这个代理方法中,但其实这个方法只是开启删除的功能,而已我们可以把删除的代码放到我们的handler中进行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值