swift:将自定义图像设置为UITable View中的每个部分

时间:2017-02-15 07:24:56

标签: ios uitableview swift2

我正在尝试将图像设置为UITable View中的部分。但是一旦我添加图像,该部分的标题就会消失。  这就是我的尝试:

 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if(section == 0)
    {
        return "Exam"
    }
    else if(section == 1)
    {
        return "Lab"
    }
    else if(section == 2)
    {
        return "Vital"
    }
    else if(section == 3)
    {
        return "Immunization"
    }
    return ""
}

现在,对于每个部分,我想添加相同的图像,我已经完成了这个:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


    let button = UIButton(frame: CGRectMake(5,10,15,15))  // create button
    button.tag = section
    button.center = CGPointMake(320.0, 480.0)
    // the button is image - set image
    button.setImage(UIImage(named: "icoCheckCircle"), forState: UIControlState.Normal)

    let tapOnCardCell: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(HHLabTestExaminationViewController.handleTapOnSectionImage(_:)))
    button.addGestureRecognizer(tapOnCardCell)

    return button
}

问题是当我按住按钮时,章节标题消失

1 个答案:

答案 0 :(得分:0)

//use this two delegate

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: "your expected headerview height")

    let button = UIButton(frame: CGRectMake(5,10,15,15))  // create button
    button.tag = section
    button.center = headerView.center
    headerView.addSubview(button)
    // the button is image - set image
    button.setImage(UIImage(named: "icoCheckCircle"), forState: UIControlState.Normal)

    let tapOnCardCell: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(HHLabTestExaminationViewController.handleTapOnSectionImage(_:)))
button.addGestureRecognizer(tapOnCardCell)

    return headerView
}


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return  "your expected header"
}