swift开发笔记6 - 在表格单元格中添加按钮

前端之家收集整理的这篇文章主要介绍了swift开发笔记6 - 在表格单元格中添加按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 通过重写tableview的单元格绘制方法添加按钮

2 重写获取tableview的单元格高度方法


代码如下:

 //设置日期按钮共7*6个
    override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
            let cell=super.tableView(tableView,cellForRowAtIndexPath: indexPath)
           //判断是否为目标单元格
            if indexPath.section == 1 && indexPath.row == 0{
            //设置x,y位置和宽高
            let button = UIButton(frame: CGRectMake(0,50,120,30))
            //设置按钮文字
            button.setTitle("1",forState: UIControlState.Normal)
            //设置字体颜色
            button.setTitleColor(UIColor.lightGrayColor(),forState: UIControlState.Normal)
            //设置点击事件
            button.addTarget(self,action: "onClick",forControlEvents: UIControlEvents.TouchUpInside)
            //加到单元格中
            cell .contentView.addSubview(button)
            
           }
           return cell
    }
    //因为按钮插入后会引起cell高度的变化,所以要重新设置
    override func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
 
            if indexPath.section == 1 && indexPath.row == 0{
                return 24*7
            }else{
                return super.tableView(tableView,heightForRowAtIndexPath: indexPath)
            }
    }
    ///点击日期按钮的动作
    func onClick(){
        ///弹出选中日期的打卡记录
        UIAlertView(title: "打卡情况",message: "上班:正常;下班:正常",delegate: nil,cancelButtonTitle: "确定",otherButtonTitles: "取消").show()
    }
原文链接:https://www.f2er.com/swift/325892.html

猜你在找的Swift相关文章