【问题标题】:index out of range uitableview in swift 3swift 3中的索引超出范围的uitableview
【发布时间】:2018-05-06 17:16:42
【问题描述】:

我做了以下屏幕,它有 5 个部分;在前 4 部分中,我将使用一个数组来显示一些信息,最后一部分我将使用它来显示一个带有第二个数组的画廊。

通常在方法 numberOfRowsInSection 中,当我更改为 return self.obj_empresa?.count 时,我返回“return 5”并且我的屏幕显示像第一张图片一样0,仅显示第一部分,如第二张图像。我该如何解决这个问题:

我的代码:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //Muestra el # de celdas a mostrar
        //return 5 before showed the 5 sections
        return self.obj_empresa?.count ?? 0
    }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.infoEmpresaCell, for: indexPath) as! InfoEmpresaCell
            cell.lbl_nombreEmpresa.text = self.obj_empresa?[indexPath.item].nombre_emp
            cell.lbl_sloganEmpresa.text = "Subtitle"
            cell.iv_perfilEmpresa.layer.cornerRadius = 10
            cell.iv_perfilEmpresa.layer.masksToBounds = true
            cell.selectionStyle = .none

            return cell
        }else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.redesSocialesEmpCell, for: indexPath) as! RedesSocialesEmpCell
            cell.iv_facebook.isUserInteractionEnabled = true
            let singletap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(visitar_facebook(singletap:)))
            cell.iv_facebook.addGestureRecognizer(singletap)

            cell.selectionStyle = .none
            return cell
        }else if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.ubEmpCell, for: indexPath)
            return cell
        }else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.descEmpCell, for: indexPath) as! DescEmpCell
            cell.lbl_descEmpresa.text = "Description"
            cell.selectionStyle = .none
            return cell
        }else if indexPath.row == 4 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.promoEmpCell, for: indexPath) as! PromoEmpCell
            cell.selectionStyle = .none
            return cell
        }
        return UITableViewCell()
    }

当我在 numberOfRowsInSection 中返回 5 并设置此 cell.lbl_nombreEmpresa.text = self.obj_empresa?[indexPath.item].nombre_emp I get this "error index out of range" in my cellForRowAt indexPath but if I return this self.obj_empresa?.count ?? 0 and I set thiscell.lbl_nombreEmpresa.text = self.obj_empresa?[indexPath.item].nombre_emp

我没有错误,但我的屏幕只显示第二张图片

第一张图片

第二张图片

提前致谢

【问题讨论】:

  • 你说得对,我的朋友谢谢:D
  • 太棒了!接受我的回答,干杯:)

标签: arrays swift uitableview swift3 swifty-json


【解决方案1】:

您显然没有正确实现 cellForRowAt。您还需要在条件句中使用 indexPath.section 。还要确保你正在实现 numberOfSections 数据源方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2018-05-07
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多