Ios UITableView在重新加载数据时向上滚动

Ios UITableView在重新加载数据时向上滚动,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,如果它的某些单元格中的按钮被点击,它会被重新加载。如果执行以下步骤,则会出现问题: 点击单元格上的按钮,以便在重新加载数据时,另一个单元格显示在点击的单元格下方。 向上滚动表格视图,使其隐藏部分上部内容。 再次点击按钮可隐藏刚刚显示的再次调用reloadData的单元格。 然后,表格视图向上显示并隐藏上部内容—整个第一个单元格和第二个单元格的一部分。下面是一些代码: func tableView(_ tableView: UITableView, heightFo

我有一个UITableView,如果它的某些单元格中的按钮被点击,它会被重新加载。如果执行以下步骤,则会出现问题:

点击单元格上的按钮,以便在重新加载数据时,另一个单元格显示在点击的单元格下方。 向上滚动表格视图,使其隐藏部分上部内容。 再次点击按钮可隐藏刚刚显示的再次调用reloadData的单元格。 然后,表格视图向上显示并隐藏上部内容—整个第一个单元格和第二个单元格的一部分。下面是一些代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if shouldShowImageResolutionOptions && (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4) {
        return isLastKnownDeviceOrientationLandscape ? 60 : 80
    }
    if shouldShowImageDisplayOptions && (indexPath.row == 3 || indexPath.row == 4) {
        return isLastKnownDeviceOrientationLandscape ? 60 : 80
    }
    return isLastKnownDeviceOrientationLandscape ? tableView.frame.size.height / 2.5 + 40 : tableView.frame.size.height / 4.5 + 40
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if shouldShowImageResolutionOptions {
        return 6
    }
    if shouldShowImageDisplayOptions {
        return 5
    }
    return 3
}

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

    switch indexPath.row {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.IntervalCell) as! IntervalCell

        cell.selectionStyle = .none
        cell.dottedSliderView.contentMode = .redraw
        cell.adjustThumbPosition()

        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
        cell.selectionStyle = .none

        cell.titleLabel.text = LabelTitles.ImageResolution
        cell.choiseLabel.text = LabelTitles.HDResolutioin
        cell.onButtonTap = {
            self.shouldShowImageResolutionOptions = !self.shouldShowImageResolutionOptions
            self.shouldShowImageDisplayOptions = false
            self.menuTableView.reloadData()
        }

        return cell
    case 2:
        if(shouldShowImageResolutionOptions) {
            let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath) as! SingleSettingCell
            cell.selectionStyle = .none
            cell.mainSettingLabel.text = Settings.HDResolution

            return cell
        }

        let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
        cell.selectionStyle = .none

        cell.titleLabel.text = LabelTitles.ImageDisplay
        cell.choiseLabel.text = LabelTitles.EnlargeImage
        cell.onButtonTap = {
            self.shouldShowImageDisplayOptions = !self.shouldShowImageDisplayOptions
            self.shouldShowImageResolutionOptions = false
            self.menuTableView.reloadData()
        }

        return cell

    case 3, 4:

        if(shouldShowImageResolutionOptions) {
            let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath)  as! SingleSettingCell
            cell.selectionStyle = .none
            cell.mainSettingLabel.text = indexPath.row == 3 ? Settings.HighResolution : Settings.MediumResolution

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath)  as! SingleSettingCell
            cell.selectionStyle = .none
            cell.mainSettingLabel.text = indexPath.row == 3 ? Settings.ShowFullImage : Settings.EnlargeImage

            return cell
        }

    case 5:

        let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
        cell.selectionStyle = .none

        cell.titleLabel.text = LabelTitles.ImageDisplay
        cell.choiseLabel.text = LabelTitles.EnlargeImage
        cell.onButtonTap = {
            self.shouldShowImageDisplayOptions = !self.shouldShowImageDisplayOptions
            self.shouldShowImageResolutionOptions = false
            self.menuTableView.reloadData()
        }

        return cell

    default:
        return UITableViewCell()
    }

}
从UITableView的重载数据方法文档中:

当表视图希望表视图完全重新加载其数据时,表视图的委托或数据源调用此方法。不应在插入或删除行的方法中调用它,尤其是在通过调用BeginUpdate和EndUpdate实现的动画块中

有专门的插入/删除行方法用于插入和删除:

InsertRowsAndExpaths:WithRowsAnimation:

deleteRowsAtIndexPaths:WithRowsAnimation:


因此,当您重构代码以使用这些视图时,它应该能够顺利地工作,并符合预期。

您应该使用@user3581248,而不是重新加载表视图,谢谢您的想法,稍后我会尝试一下,并告诉它是否修复了它。@user3581248我用insertRowsAtIndexPaths:withRowAnimation:和deleteRowsAtIndexPaths:withRowAnimation:更新了我的代码,它修复了这个问题。你知道为什么在重新加载数据时这样做吗?如果你把你的建议作为答复,我会接受的。