Rahul Gupta
Rahul Gupta

Reputation: 539

Uibutton issue in swift 3.0

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()

    if isFacility {
        cell = tableView.dequeueReusableCell(withIdentifier: "facilityCell")!
        let gameNameLbl = cell.viewWithTag(100) as! UILabel
        gameNameLbl.text = "Badminton Court"
        let gameImgVw = cell.viewWithTag(101) as! UIImageView
        let bookNowBtn = cell.viewWithTag(102) as! UIButton
        bookNowBtn.backgroundColor = DEFAULTAPPCOLOR
        ConstantFile.makeCornerRoundBtnWithOutBorder(btn: bookNowBtn)
        bookNowBtn.tag = indexPath.row
        bookNowBtn.addTarget(self, action: #selector(bookCort(sender:)), for: .touchUpInside)


        bookNowBtn.setTitle("BOOK NOW", for: .normal)
        if indexPath.row % 2 == 0 {
            gameImgVw.backgroundColor = UIColor.red
        }
        else{
            gameImgVw.backgroundColor = UIColor.darkText
        }
    }
    else{
        cell = tableView.dequeueReusableCell(withIdentifier: "bookingCell")!
        let gameImgVw = cell.viewWithTag(103) as! UIImageView
        let gameNameLbl = cell.viewWithTag(104) as! UILabel
        let gameDateLbl = cell.viewWithTag(105) as! UILabel
        let gametimeLbl = cell.viewWithTag(106) as! UILabel
        ConstantFile.roundImageViewWithOutBorder(image: gameImgVw)
        gameNameLbl.text = "Tennis Court"
        gameDateLbl.text = "22 Dec 2017"
        gametimeLbl.text = "7:00pm - 9:00pm"
    }
    return cell

}

In the above code it's giving me crash while scrolling the table "fatal error: unexpectedly found nil while unwrapping an Optional value" for uibutton (in this line let bookNowBtn = cell.viewWithTag(102) as! UIButton).

Upvotes: 0

Views: 150

Answers (1)

Anuraj
Anuraj

Reputation: 1240

May be u have u put different tag value in storyBoard.

Upvotes: 0

Related Questions