UITableView基础[ 6 ] 自定义RowAction

本文介绍了如何在iOS应用中自定义UITableViewRowAction,通过实现tableView(editActionsForRowAtIndexPath:)方法,可以为UITableViewCell添加如置顶等自定义操作,提高了UI交互的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

上节我们说到了如何实现单元格删除的功能,但是很多时候我们需要更多复杂的功能,而不仅仅是删除这样的简单操作,例如QQ好友列表中的置顶等功能,这时候我们就需要进行自定义RowAction

实现

要自定义RowAction,我们需要实现

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

注意:实现这个方法会使得自带的删除功能失效,需要重写
首先创建一个UITableViewRowAction

利用其构造器

public convenience init(style: UITableViewRowActionStyle, title: String?, handler: (UITableViewRowAction, NSIndexPath) -> Void)

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action, indexPath) -> Void in
        let alert = UIAlertController(title: "Share", message: "taped Share Action", preferredStyle: .Alert)
        let cancel = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
        alert.addAction(cancel)
        self.presentViewController(alert, animated: true, completion: nil)
    }
    return [shareAction]
}

图片描述
图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值