ios - swift 3.0 中的 Uibutton 问题

标签 ios swift uibutton

    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

}

在上面的代码中,它在滚动 uibutton 的表“ fatal error :在展开可选值时意外发现 nil”时崩溃(在这一行中让 bookNowBtn = cell.viewWithTag(102) as! UIButton)。

最佳答案

也许你在storyBoard中放置了不同的标签值。

关于ios - swift 3.0 中的 Uibutton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335625/

相关文章:

ios - CLLocationManager 的委托(delegate)函数 'didRangeBeacon' 没有找到任何信标?

ios - 如何在 swift 4.0 中将值附加到数组后重新加载 tableView

ios - 有没有一种简单的方法可以从图像中绘制 SKnodes 障碍物的坐标?

ios - 禁止 ios 通过本地化更改 GUI Controller 位置

ios - 如何在 uicollectionview 上添加阴影?

ios - 加快/减少 Swift 中绘制位图的 CPU 负载

ios - 线程 1 :signal SIGBART

ios - 作为 MTKView subview 的 UIView 是否在 GPU 上处理/渲染?

ios - 在 UIView 加载后加载 UIButton

iphone - 如何创建像 MobilePASS 一样的自定义圆角 UiView 和 UIButton?