在循环中添加UIGestureRecognizer会产生问题

时间:2015-11-25 07:42:43

标签: ios swift uitableview uilabel uigesturerecognizer

我使用UITableView,我的UITableViewCells标签和高度不同,我需要能够响应标签上的点按。

cellForRowAtIndexPath我在循环中向标签添加了所需的字段和多个UIGestureRecognizers,但是UIGestureRecognizer仅响应单元格的前3行,这是我的代码和屏幕截图。

新代码:我在单元格中添加了一个按钮,但无法看到它

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("gameCell", forIndexPath: indexPath)

    // Remove all subViews frm cell
    for subView in cell.subviews {
        subView.removeFromSuperview()
    }
    for(var i = 0; i < 7;i++){
        let gameView = UIView(frame: CGRectMake(0,CGFloat(i * 50) ,cell.frame.width,50))
        let lblHcapHome = UILabel(frame: CGRectMake(10,CGFloat(i * 50),100, 30))
        lblHcapHome.layer.borderWidth = 1
        lblHcapHome.textColor = UIColor.blueColor()
        lblHcapHome.text = "xxxx"
        lblHcapHome.userInteractionEnabled = true
        lblHcapHome.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblHcapHomeTaped:"))
        gameView.addSubview(lblHcapHome)

        let btn = UIButton(frame:CGRectMake(lblHcapHome.frame.origin.y + lblHcapHome.frame.width + CGFloat(10),CGFloat(i * 50) ,cell.frame.width,50))
        btn.titleLabel?.text = "uuuuuuuuuuu"
        gameView.addSubview(btn)

cell.addSubview(gameView)
    }

    return cell
}
   let cell = self.tableView.dequeueReusableCellWithIdentifier("gameCell",forIndexPath: indexPath) as UITableViewCell

    // Remove all subViews frm cell
    for subView in cell.subviews {
        subView.removeFromSuperview()
    }
        for(var i = 0;i <= NumberOfLines; i++){
        print("indexInGamesArray: ",indexInGamesArray)
        lineMultiplayer = CGFloat(i)

        let gameView = UIView(frame: CGRectMake(0,CGFloat(i * 50) + lblTL.frame.origin.y + lblTL.frame.height ,cell.frame.width,50))
        //***** 1 First Line
        lblH = UILabel(frame: CGRectMake(0,lineMultiplayer * 32,teamSignWidth,32))
        print ("blH.frame.origin.y: ",lblH.frame.origin.y)
        lblH.text = " H"
        lblH.textColor = PunteamConstants.lblColorSTD
        lblH.font = PunteamConstants.smallBoldFont
        gameView.addSubview(lblH)
        //***** 2
        lblHcapLine = UILabel(frame: CGRectMake(lblH.frame.origin.x + lblH.frame.width, lblH.frame.origin.y,btnWidth,32))
        lblHcapLine.layer.borderColor = PunteamConstants.lblColorSTD.CGColor
        lblHcapLine.layer.borderWidth = 1
        lblHcapLine.textColor = PunteamConstants.lblColorSTD
        lblHcapLine.font = PunteamConstants.stdLblFont
        lblHcapLine.text = (gamesArray[indexInGamesArray]["HcapLine"])!!.description
        lblHcapLine.textAlignment = NSTextAlignment.Center
        gameView.addSubview(lblHcapLine)
        //***** 3
        lblHcapHome = UILabel(frame: CGRectMake(lblHcapLine.frame.origin.x + lblHcapLine.frame.width + gapWidth,
            lblH.frame.origin.y,btnWidth, 32))
        lblHcapHome.layer.borderWidth = 1
        lblHcapHome.textColor = UIColor.blueColor()
        lblHcapHome.font = PunteamConstants.stdLblFont
        var underlineAttriString = NSAttributedString(string:(gamesArray[indexInGamesArray]["HcapOddsHome"])!!.description, attributes:
            [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])

        lblHcapHome.tag = indexInGamesArray
        lblHcapHome.userInteractionEnabled = true

        lblHcapHome.attributedText = underlineAttriString
        lblHcapHome.textAlignment = NSTextAlignment.Center
        lblHcapHome.backgroundColor = UIColor(patternImage: UIImage(named: "select-button_Transparent")!)
        lblHcapHome.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblHcapHomeTaped:"))
        gameView.addSubview(lblHcapHome)

        cell.addSubview(gameView)

}         返回单元格

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cellArrayIndex = cellsPointerArray[indexPath.row] as! Int

    let cell = self.tableView.dequeueReusableCellWithIdentifier("gameCell",forIndexPath: indexPath) as UITableViewCell

    // Remove all subViews frm cell
    for subView in cell.subviews {
        subView.removeFromSuperview()
    }

    indexInGamesArray = 0
    let btnWidth = CGFloat((cell.frame.width / 100) * 20)
    let teamSignWidth = CGFloat((cell.frame.width / 100) * 4)
    let gapWidth = CGFloat((cell.frame.width / 100) * 3)

    //***** fixet fields iv every cell
    let lblTop = UILabel(frame: CGRectMake(0 ,0,self.frame.size.width, 25))
    lblTop.backgroundColor = UIColor(red: 88/255, green: 88/255, blue: 88/255, alpha: 1)
    cell.addSubview(lblTop)

    let lblDate = UILabel(frame: CGRectMake(lblTop.frame.origin.x + 30.0,lblTop.frame.origin.y,100,lblTop.frame.size.height))
    lblDate.backgroundColor = UIColor.clearColor()

    let lblTime = UILabel(frame: CGRectMake(lblDate.frame.origin.x + lblDate.frame.size.width + 30.0,lblTop.frame.origin.y,300,lblTop.frame.size.height))
    lblTime.backgroundColor = UIColor.clearColor()

    let str : String = (gamesArray[cellArrayIndex]["GameDate"] as? String)!
    let characterToFind: Character = "T"
    let indexForCharacterInString = str.characters.indexOf(characterToFind)
    let dateStr:String = str.substringToIndex(indexForCharacterInString!)
    lblDate.text = dateStr
    lblDate.textColor = UIColor.yellowColor()
    cell.addSubview(lblDate)

    var timeStr:String = str.substringFromIndex(indexForCharacterInString!)
    timeStr.removeAtIndex(str.characters.indices.first!)
    lblTime.text = timeStr
    lblTime.textColor = UIColor.yellowColor()
    cell.addSubview(lblTime)

    let lblHomName = UILabel(frame: CGRectMake(20,lblDate.frame.origin.y + lblDate.frame.size.height,self.frame.size.width/2 - 30,25))
    lblHomName.font = PunteamConstants.smallFont
    lblHomName.text = gamesArray[cellArrayIndex]["HomeName"] as? String


    let HcapLine = gamesArray[cellArrayIndex]["HcapLine"] as? Int
    if(HcapLine < 0){
        lblHomName.textColor = UIColor.redColor()
    }
    else{
        lblHomName.textColor = PunteamConstants.lblColorSTD
    }
    cell.addSubview(lblHomName)

    let lblAwayName = UILabel(frame: CGRectMake(self.frame.width/2 + 20,lblDate.frame.origin.y + lblDate.frame.size.height,self.frame.size.width/2 - 30,25))
    lblAwayName.font = PunteamConstants.smallFont
    lblAwayName.text = gamesArray[cellArrayIndex]["AwayName"] as? String

    if(HcapLine < 0){
        lblAwayName.textColor = UIColor.redColor()
    }
    else{
        lblAwayName.textColor = PunteamConstants.lblColorSTD
    }
    cell.addSubview(lblAwayName)

    let lblHC = UILabel(frame: CGRectMake(cell.frame.width/2 - 80,lblAwayName.frame.origin.y + lblAwayName.frame.size.height,self.frame.size.width/2 - 30,25))
    lblHC.text = "Handicap"
    lblHC.textColor = UIColor.redColor()
    lblHC.font = PunteamConstants.smallBoldFont
    cell.addSubview(lblHC)

    let lblTL = UILabel(frame: CGRectMake(cell.frame.width/2 + 10,lblAwayName.frame.origin.y + lblAwayName.frame.size.height,self.frame.size.width/2 - 30,25))
    lblTL.text = "Total Line"
    lblTL.textColor = UIColor.redColor()
    lblTL.font = PunteamConstants.smallBoldFont
    cell.addSubview(lblTL)

    //***** end if fixed fields block

    //***************************************
    //***** Build the lines of the cell *****
    //***************************************
    print("gamesArray.count: ",gamesArray.count)
    let NumberOfLines = numOfGamesArray[indexPath.row] as! Int

    indexInGamesArray = (cellsPointerArray[indexPath.row] as! Int)

    for(var i = 0;i <= NumberOfLines; i++){
        print("indexInGamesArray: ",indexInGamesArray)
        lineMultiplayer = CGFloat(i)

        let gameView = UIView(frame: CGRectMake(0,CGFloat(i * 50) + lblTL.frame.origin.y + lblTL.frame.height ,cell.frame.width,50))
        //***** 1 First Line
        lblH = UILabel(frame: CGRectMake(0,lineMultiplayer * 32,teamSignWidth,32))
        print ("blH.frame.origin.y: ",lblH.frame.origin.y)
        lblH.text = " H"
        lblH.textColor = PunteamConstants.lblColorSTD
        lblH.font = PunteamConstants.smallBoldFont
        gameView.addSubview(lblH)
        //***** 2
        lblHcapLine = UILabel(frame: CGRectMake(lblH.frame.origin.x + lblH.frame.width, lblH.frame.origin.y,btnWidth,32))
        lblHcapLine.layer.borderColor = PunteamConstants.lblColorSTD.CGColor
        lblHcapLine.layer.borderWidth = 1
        lblHcapLine.textColor = PunteamConstants.lblColorSTD
        lblHcapLine.font = PunteamConstants.stdLblFont
        lblHcapLine.text = (gamesArray[indexInGamesArray]["HcapLine"])!!.description
        lblHcapLine.textAlignment = NSTextAlignment.Center
        gameView.addSubview(lblHcapLine)
        //***** 3
        lblHcapHome = UILabel(frame: CGRectMake(lblHcapLine.frame.origin.x + lblHcapLine.frame.width + gapWidth,
            lblH.frame.origin.y,btnWidth, 32))
        lblHcapHome.layer.borderWidth = 1
        lblHcapHome.textColor = UIColor.blueColor()
        lblHcapHome.font = PunteamConstants.stdLblFont
        var underlineAttriString = NSAttributedString(string:(gamesArray[indexInGamesArray]["HcapOddsHome"])!!.description, attributes:
            [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])

        lblHcapHome.tag = indexInGamesArray
        lblHcapHome.userInteractionEnabled = true

        lblHcapHome.attributedText = underlineAttriString
        lblHcapHome.textAlignment = NSTextAlignment.Center
        lblHcapHome.backgroundColor = UIColor(patternImage: UIImage(named: "select-button_Transparent")!)
        lblHcapHome.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblHcapHomeTaped:"))
        gameView.addSubview(lblHcapHome)
        //***** 4
        let lblO = UILabel(frame: CGRectMake(lblHcapHome.frame.origin.x + lblHcapHome.frame.width + 5,
            lineMultiplayer * 32,teamSignWidth,32))
        lblO.text = " O"
        lblO.textColor = PunteamConstants.lblColorSTD
        lblO.font = PunteamConstants.smallBoldFont
        gameView.addSubview(lblO)
        //***** 5
        lblTotalLine = UILabel(frame: CGRectMake(lblO.frame.origin.x + lblO.frame.width,
            lblO.frame.origin.y,btnWidth, 32))
        lblTotalLine.layer.borderWidth = 1
        lblTotalLine.textColor = PunteamConstants.lblColorSTD
        lblTotalLine.font = PunteamConstants.stdLblFont
        lblTotalLine.text = (gamesArray[indexInGamesArray]["TotalLine"])!!.description

        if(lblTotalLine.text != "<null>"){
            lblTotalLine.textAlignment = NSTextAlignment.Center
             gameView.addSubview(lblTotalLine)
        }
        else{
            lblO.removeFromSuperview()
        }
        //***** 6
        lblOverOdds = UILabel(frame: CGRectMake(lblTotalLine.frame.origin.x + lblTotalLine.frame.width + gapWidth,
            lblH.frame.origin.y,btnWidth, 32))
        lblOverOdds.layer.borderWidth = 1
        lblOverOdds.textColor = UIColor.blueColor()
        lblOverOdds.font = PunteamConstants.stdLblFont

        var strTest = (gamesArray[indexInGamesArray]["OverOdds"])??.description

        if(strTest != "<null>"){
            lblOverOdds.tag = indexInGamesArray
            lblOverOdds.userInteractionEnabled = true
            underlineAttriString = NSAttributedString(string:(gamesArray[indexInGamesArray]["OverOdds"])!!.description, attributes:
                [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
            lblOverOdds.attributedText = underlineAttriString

            lblOverOdds.textAlignment = NSTextAlignment.Center
            lblOverOdds.backgroundColor = UIColor(patternImage: UIImage(named: "select-button_Transparent")!)
            lblOverOdds.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblOverOddsTaped:"))
            gameView.addSubview(lblOverOdds)
        }


        //***** 1 Second line
        lblA = UILabel(frame: CGRectMake(0,lblH.frame.origin.y + lblH.frame.height + 5,teamSignWidth,32))
        print ("blH.frame.origin.y: ",lblH.frame.origin.y)
        lblA.text = " A"
        lblA.textColor = PunteamConstants.lblColorSTD
        lblA.font = PunteamConstants.smallBoldFont
        gameView.addSubview(lblA)
        //***** 2
        var hccapLine1 = gamesArray[indexInGamesArray]["HcapLine"] as! Float
        hccapLine1 = hccapLine1 * -1

        lblHcapLine1 = UILabel(frame: CGRectMake(lblH.frame.origin.x + lblH.frame.width, lblA.frame.origin.y,btnWidth,32))
        lblHcapLine1.layer.borderColor = PunteamConstants.lblColorSTD.CGColor
        lblHcapLine1.layer.borderWidth = 1
        lblHcapLine1.textColor = PunteamConstants.lblColorSTD
        lblHcapLine1.font = PunteamConstants.stdLblFont
        lblHcapLine1.text = hccapLine1.description
        lblHcapLine1.textAlignment = NSTextAlignment.Center
        gameView.addSubview(lblHcapLine1)

        //***** 3
        lblHcapOddsAway = UILabel(frame: CGRectMake(lblHcapLine1.frame.origin.x + lblHcapLine1.frame.width + gapWidth,
            lblA.frame.origin.y,btnWidth, 32))
        lblHcapOddsAway.layer.borderWidth = 1
        lblHcapOddsAway.textColor = UIColor.blueColor()
        lblHcapOddsAway.font = PunteamConstants.stdLblFont
        underlineAttriString = NSAttributedString(string:((gamesArray[indexInGamesArray]["HcapOddsAway"])??.description)!, attributes:
            [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])

        lblHcapOddsAway.tag = indexInGamesArray
        lblHcapOddsAway.userInteractionEnabled = true

        lblHcapOddsAway.attributedText = underlineAttriString
        lblHcapOddsAway.textAlignment = NSTextAlignment.Center
        lblHcapOddsAway.backgroundColor = UIColor(patternImage: UIImage(named: "select-button_Transparent")!)
        lblHcapOddsAway.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblHcapOddsAwayTaped:"))
        gameView.addSubview(lblHcapOddsAway)

        //***** 4
        lblU = UILabel(frame: CGRectMake(lblHcapOddsAway.frame.origin.x + lblHcapOddsAway.frame.width + 5,
            lblA.frame.origin.y,teamSignWidth,32))
        lblU.text = " U"
        lblU.textColor = PunteamConstants.lblColorSTD
        lblU.font = PunteamConstants.smallBoldFont
        gameView.addSubview(lblU)
        //***** 5

        lblTotalLine1 = UILabel(frame: CGRectMake(lblU.frame.origin.x + lblU.frame.width,
            lblU.frame.origin.y,btnWidth, 32))
        lblTotalLine1.layer.borderWidth = 1
        lblTotalLine1.textColor = PunteamConstants.lblColorSTD
        lblTotalLine1.font = PunteamConstants.stdLblFont
        lblTotalLine1.text = (gamesArray[indexInGamesArray]["TotalLine"])??.description

        print("lblTotalLine1.text:", lblTotalLine1.text)
        if(lblTotalLine1.text != "<null>"){

            lblTotalLine1.tag = indexInGamesArray
            lblTotalLine1.textAlignment = NSTextAlignment.Center
            gameView.addSubview(lblTotalLine1)

        }
        else{
            lblU.removeFromSuperview()
        }
        //***** 6
        lblUnderOdds = UILabel(frame: CGRectMake(lblTotalLine1.frame.origin.x + lblTotalLine1.frame.width + gapWidth,
            lblU.frame.origin.y,btnWidth, 32))
        lblUnderOdds.layer.borderWidth = 1
        lblUnderOdds.textColor = UIColor.blueColor()
        lblUnderOdds.font = PunteamConstants.stdLblFont

        strTest = (gamesArray[indexInGamesArray]["UnderOdds"])??.description
        if("<null>" != strTest){
            lblUnderOdds.tag = indexInGamesArray
            underlineAttriString = NSAttributedString(string:((gamesArray[indexInGamesArray]["UnderOdds"])??.description)!, attributes:
                [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
            lblUnderOdds.userInteractionEnabled = true
            lblUnderOdds.attributedText = underlineAttriString

            lblUnderOdds.textAlignment = NSTextAlignment.Center
            lblUnderOdds.backgroundColor = UIColor(patternImage: UIImage(named: "select-button_Transparent")!)
            lblUnderOdds.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "lblUnderOddsTaped:"))
            gameView.addSubview(lblUnderOdds)
        }


        //****************************
        cell.addSubview(gameView)
        indexInGamesArray++

    }
    cellArrayIndex = 0

    //***** Build the lines of the cell block end
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}

0 个答案:

没有答案