附加CellType索引范围错误误差swift

发布于 2025-02-03 15:32:40 字数 1453 浏览 3 评论 0原文

在附加新的CellType后,我的位置TableViewCell在输入文本字段搜索栏内输入文本时,在Let plote = place = place = place = place = place = place = place = place = place = place = place = place = place = place place = place = ploteviewcell之后。

private func locationTableViewCell(indexPath: IndexPath) -> PostSelectLocationTableViewCell {
    let locationCell = tableView.dequeueReusableCell(forIndexPath: indexPath) as PostSelectLocationTableViewCell
    let place = places[indexPath.row]
    locationCell.configure(model: place)
    return locationCell
}

locationInteractor.didPlacesLoaded = { [weak self] places in
        DispatchQueue.main.async {
            self?.places = places
            if self?.places.isEmpty == true {
                self?.cellTypes = [.empty]
            } else {
                self?.cellTypes += Array(repeating: .location, count: self?.places.count ?? 0)
            }
            self?.tableView.reloadData()
        }
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTypes.count
}

在添加。含量之前,一切都是桃子。但是现在,当我输入在位置之间搜索的文本时,它会以索引不超出范围错误崩溃。我试图让plote = plote [indexpath.row + 1],让plote = plote = plote [indexpath.row -1],但无效。

locationInteractor.didCurrrentPlaceLoaded = { [weak self] currentPlace in
        self?.currentLocation = currentPlace
        self?.cellTypes.append(.currentLocation)
        self?.tableView.reloadData()
    }

After appending a new cellType my locationTableViewCell is giving an error at let place = places[indexPath.row] while entering text inside the text field search bar.

private func locationTableViewCell(indexPath: IndexPath) -> PostSelectLocationTableViewCell {
    let locationCell = tableView.dequeueReusableCell(forIndexPath: indexPath) as PostSelectLocationTableViewCell
    let place = places[indexPath.row]
    locationCell.configure(model: place)
    return locationCell
}

locationInteractor.didPlacesLoaded = { [weak self] places in
        DispatchQueue.main.async {
            self?.places = places
            if self?.places.isEmpty == true {
                self?.cellTypes = [.empty]
            } else {
                self?.cellTypes += Array(repeating: .location, count: self?.places.count ?? 0)
            }
            self?.tableView.reloadData()
        }
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTypes.count
}

before appending .currentLocation everything was peachy. But now when I enter a text to search between locations, it crashes with Index Out Of Range error. I have tried to let place = places[indexPath.row + 1] and let place = places[indexPath.row - 1] but it was ineffective.

locationInteractor.didCurrrentPlaceLoaded = { [weak self] currentPlace in
        self?.currentLocation = currentPlace
        self?.cellTypes.append(.currentLocation)
        self?.tableView.reloadData()
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2025-02-10 15:32:40

问题在于我以前的数字如下。

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellTypes.count
}

因此,这导致我索引外部错误。

在更改NumberRowsInsection之后,对于每个特定的CellType,我的错误就消失了。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //  sponsored by Serhat Sezen
        if !places.isEmpty {
            return places.count
        } else {
            return cellTypes.count
        }
    }

The problem was that my previous numberOfRowsInSection was as below.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellTypes.count
}

so this was causing me index out-of-range error.

After changing numberOfRowsInSection as below for each specific celltype my error is gone.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //  sponsored by Serhat Sezen
        if !places.isEmpty {
            return places.count
        } else {
            return cellTypes.count
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文