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