加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中UITableView的使用(左滑多按钮)

发布时间:2020-12-14 06:38:27 所属栏目:百科 来源:网络整理
导读: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(Strin

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这个方法,极大的方便于了开发者自定义多按钮响应。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读