swift中UITableView的使用左滑多按钮

github学习地址:https://github.com/potato512/SYSwiftLearning

效果


源码

// MARK: - 数据
func setLocalData()
{
        self.mainArray = NSMutableArray()
        
        for number in 1...10
        {
            let numberTmp = random() % 1000 + number
            self.mainArray.addobject(String(numberTmp))
        }
}
// MARK: - 视图
func setUI()
{
        self.mainTableView = UITableView(frame: self.view.bounds,style: .Plain)
        self.view.addSubview(self.mainTableView)
        self.mainTableView.backgroundColor = UIColor.clearColor()
        self.mainTableView.delegate = self
        self.mainTableView.dataSource = self
        self.mainTableView.autoresizingMask = UIViewAutoresizing.FlexibleHeight
        self.mainTableView.tableFooterView = UIView()
}
// MARK: - UITableViewDataSource,UITableViewDelegate
    
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
}
    
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return self.mainArray.count
}
    
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
        if cell == nil
        {
            cell = UITableViewCell(style: .Default,reuseIdentifier: "UITableViewCell")
        }
        
        let text = self.mainArray[indexPath.row] as! String
        cell.textLabel!.text = text
        
        return cell
}
    
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath,animated: true)
}
// 更多按钮设置
func tableView(tableView: UITableView,editactionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
        let top = UITableViewRowAction(style: .normal,title: "置顶") {
            action,index in
            print("more button tapped")
            self.alertShow("置顶")
        }
        top.backgroundColor = UIColor.lightGrayColor()
        
        let readed = UITableViewRowAction(style: .normal,title: "标为已读") {
            action,index in
            print("favorite button tapped")
            self.alertShow("标为已读")
        }
        readed.backgroundColor = UIColor.orangeColor()
        
        let delete = UITableViewRowAction(style: .normal,title: "删除") {
            action,index in
            print("share button tapped")
            self.alertShow("删除")
        }
        delete.backgroundColor = UIColor.blueColor()
        
        return [top,readed,delete]
}
// MARK: - alert
    
func alertShow(title:String)
{
        let alertView = UIAlertView(title: nil,message: title,delegate: nil,cancelButtonTitle: "知道了");
        alertView.show()
}

说明:在iOS8.0之后,Apple开放了editactionsForRowAtIndexPath这个方法,极大的方便于了开发者自定义多按钮响应。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...