UITableView在reloadData上滚动

时间:2018-07-25 14:31:25

标签: ios swift uitableview

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

  1. 点击一个单元格上的按钮,以便另一个单元格出现在reloadData上所点击的单元格下方。
  2. 向上滚动表格视图,使其隐藏一些上层内容。
  3. 再次点击按钮以隐藏刚刚显示的再次调用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()
    }

}

1 个答案:

答案 0 :(得分:1)

摘自UITableView的{​​{1}}方法文档(https://developer.apple.com/documentation/uikit/uitableview/1614862-reloaddata):

  

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

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

因此,当您重构代码以使用这些代码时,它应该可以正常工作并按预期工作。