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

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

发布时间:2020-12-14 01:47:48 所属栏目:百科 来源:网络整理
导读:1 通过重写tableview的单元格绘制方法来添加按钮 2 重写获取tableview的单元格高度方法 代码如下: //设置日期按钮共7*6个 override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) - UITableViewCell { let cell=su

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

(编辑:李大同)

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

    推荐文章
      热点阅读