当在swift中点击视图时,如何在viewForHeaderInSection中更改视图的背景颜色

时间:2015-07-14 10:38:44

标签: swift uitableview uitapgesturerecognizer

我正在使用可扩展的表格视图。我使用了以下逻辑。如果我点击该部分,我无法更改视图的背景颜色。我需要在点击时更改各自视图的背景颜色。根据我的代码,红色闪烁。我不知道为什么?请指导我。

对于Ex,我有5个部分,它显示5个视图。我无法为每个视图添加标记。我不知道该怎么做。请指导我。

我的编码:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          var headerView = UIView(frame: CGRectMake(0, 0, sideTableView.frame.size.width, 49))
            var layerLine = CALayer()
            layerLine.frame = CGRect(x: 0, y: headerView.frame.size.height, width:  headerView.frame.size.width, height: 1)
            layerLine.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0).CGColor
            headerView.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 242/255, alpha: 1.0)
            headerView.tag = section

            headerView.layer.addSublayer(layerLine)

            var headerString = UILabel(frame: CGRect(x: 10, y: 10, width: sideTableView.frame.size.width-10, height: 30)) as UILabel
            headerString.text = sideTblData.objectAtIndex(section) as! NSString as String
            headerString.font = headerString.font.fontWithSize(15)
            headerView .addSubview(headerString)

            headerView.tag = section

            let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
            headerView .addGestureRecognizer(headerTapped)

            return headerView

    }

func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {

if(cellClickedBool == true)
        {
            if(recognizer.view?.tag == 1)
            {
                recognizer.view?.backgroundColor = UIColor.redColor() //RED COLOR IS BLINKING

            }

            cellClickedBool = false
        }
        else
        {
recognizer.view?.backgroundColor = UIColor.whiteColor()

            cellClickedBool = true
        }
}

0 个答案:

没有答案