ios - 重新加载数据后 UIScrollView 崩溃

标签 ios swift uitableview uiscrollview

我在 tableView 的每一行中都有 ScrollView。我正在为其分配不同的标签。 它第一次完美运行。当更改数组中的数据并使 ReloadData 应用程序崩溃时。

错误就像

fatal error: unexpectedly found nil while unwrapping an Optional value

一段代码

 cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell", for: indexPath)
                let lblName = (cell.viewWithTag(31)! as! UILabel)
                let lblSubmited = (cell.viewWithTag(32)! as! UILabel)
                let lblFrom = (cell.viewWithTag(33)! as! UILabel)
                let lblTo = (cell.viewWithTag(34)! as! UILabel)
                let lblTotalHours = (cell.viewWithTag(35)! as! UILabel)
                let lblapprove = (cell.viewWithTag(36)! as! UILabel)

                lblName.text = dict.object(forKey: "name") as? String
                lblSubmited.text = dict.object(forKey: "submitted") as? String
                lblFrom.text = dict.object(forKey: "from") as? String
                lblTo.text = dict.object(forKey: "to") as? String
                lblTotalHours.text = dict.object(forKey: "tot") as? String
                lblapprove.text = "Pending"

                let scrollView:UIScrollView!
                if(flag.object(at: indexPath.row) as! String == "0"){
                        scrollView = (cell.viewWithTag(3)! as! UIScrollView)
                        scrollView.tag = ((100 * indexPath.row)+1)
                        flag.replaceObject(at: indexPath.row, with: "1")
                }
                else{
                    scrollView = (cell.viewWithTag(((100 * indexPath.row)+1))! as! UIScrollView)
                }

            scrollView.delegate = self
            scrollView.showsHorizontalScrollIndicator = false

            let viewContent = (cell.viewWithTag(1000)!)
            let btApprove = UIButton(frame: CGRect(x: 704.0, y: 10.0, width: 40.0, height: 40.0))
            let btReject = UIButton(frame: CGRect(x: 788.0, y: 10.0, width: 40.0, height: 40.0))
            let btDetail = UIButton(frame: CGRect(x: 860.0, y: 10.0, width: 40.0, height: 40.0))

            btApprove.addTarget(self, action: #selector(self.approvePressedAction), for: .touchUpInside)
            btReject.addTarget(self, action: #selector(self.rejectPressedAction), for: .touchUpInside)
            btDetail.addTarget(self, action: #selector(self.detailPressedAction), for: .touchUpInside)

            viewContent.addSubview(btApprove)
            viewContent.addSubview(btReject)
            viewContent.addSubview(btDetail)

  return cell

出现错误

scrollView = (cell.viewWithTag(3)! as! UIScrollView)

行并在重新加载数据后崩溃。有什么解决办法吗?

最佳答案

似乎你在强制许多可选值解包,也许在尝试使用它们之前先检查它们。

我不知道您的逻辑在做什么,但要对崩溃的特定行运行一些检查,请尝试这样的操作。

var scrollView:UIScrollView!
if(flag.object(at: indexPath.row) as! String == "0"){
    if let view = cell.viewWithTag(3) {
        if let sv = view as? UIScrollView {
            scrollView = sv
            scrollView.tag = ((100 * indexPath.row)+1)
            flag.replaceObject(at: indexPath.row, with: "1")
        } else {
            print("view found with tag 3 but it's not a scrollview")
        }
    } else {
        print("no view found with tag 3")
    }
} else {
    scrollView = (cell.viewWithTag(((100 * indexPath.row)+1))! as! UIScrollView)
}

关于ios - 重新加载数据后 UIScrollView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42669556/

相关文章:

iphone - iOS 4+ : Need App to send multiple Emails with Attachments but LATER, 不是立即

swift - fatal error : when my PFUser object is unwrapped

swift - UITableViewCell 的 RemoveGestureRecognizer 方法在 Swift、ios8、Xcode 7 中不起作用

ios - 为每个 TableViewCell 实例化新数组

ios - 我想在我的应用程序中使用分页。我有一个像这样的API :

ios - 标识符为 'com.companyname.appname' 的 App ID 不可用。请输入不同的字符串

swift - 编程约束未按预期工作

ios - 在没有 IF 语句的情况下在滑动时显示和隐藏 UIView?

ios - 像气泡一样自定义 UITableViewCell

ios - 使用 CGAffineTransform swift 3 语法使按钮逆时针旋转?