微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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()
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐