ios - 点击时更改 UITableViewCell 高度时动画不稳定

标签 ios swift uitableview uianimation

UITableView 单元格高度变化在 iPad 和 iPhone 6S 上有不稳定的动画,但在 iPhone 6 上工作正常。我在侧面 UITableView 单元格中有 UIImageView(3/4 比例)和动态单元格高度(下面的代码)。我想在任何设备类型上都适合全宽图像。

此刻,我的代码适用于所有设备。问题是在 iPad 和 iPhone 6 Plus 上高度变化动画不流畅。在 iPhone 6 上,它很流畅。那么谁能告诉我我做错了什么?或者我应该怎么做才能使单元格高度变化动画在所有设备上都能正常工作?

我正在使用以下代码:

// Using PFQueryTableViewController
 let screenSize: CGRect = UIScreen.mainScreen().bounds
    var selectedCellIndexPath: NSIndexPath?
    var SelectedCellHeight = CGFloat() 
    var UnselectedCellHeight = CGFloat() 
  ....

   override func viewDidLoad() {
        super.viewDidLoad()

// This is how I am currently checking the tableView cell height on tap.
        if screenSize.width > 700 {
            SelectedCellHeight = 1000.0
            UnselectedCellHeight = 778.0
        } else {
            SelectedCellHeight = 522.0
            UnselectedCellHeight = 320.0
        }
        print(screenSize.width)
        print(screenSize.height)   
    }



override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            return SelectedCellHeight
        }
    }
    return UnselectedCellHeight
}


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell!
        if cell == nil {
            cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        }

        let sTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "sTap")
        sTap.numberOfTapsRequired = 1
        cell.addGestureRecognizer(sTap)


        // Display main image
        let initialThumbnail = UIImage(named: "DefaultImage")
        cell.postPicture.image = initialThumbnail

        if let thumbnail = object?["postOriginalPicture"] as? PFFile {
            cell.postPicture.file = thumbnail
            cell.postPicture.loadInBackground()  
        }
        return cell
    }


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell!
    if cell == nil {
        cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }
 customNSIndexPath = indexPath
}


func sTap() {  // Tap code to increases height of tableView cell

    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            self.selectedCellIndexPath = nil

        } else {
            self.selectedCellIndexPath = indexPath
        }
    } else {
        selectedCellIndexPath = indexPath
    }
    tableView.beginUpdates()
    tableView.endUpdates()
}

最佳答案

您的代码中仍然有一些奇怪的事情,我不确定它们是否解释了您所描述的行为,但它们是:

  1. 无需注册自己的手势识别器 选择, TableView 在您设置后自行执行 allowsSelection 属性。
  2. 如果您出于某种原因确实想使用自己的手势识别器, 仅将其添加到单元格一次;不要每次都加一个 细胞被重复使用。
  3. 单元格的出列/创建 在 didSelectRowAtIndexPath 中没有任何作用
  4. beginUpdates/endUpdates 之间没有任何内容的组合 不做任何事。只有当你想申请时才需要它们 对表的多次更改,全部基于旧索引并使用 组合动画
  5. 你还需要打电话 reloadRowsAtIndexPaths 告诉表格单元格的大小已更改。 在 didSelectRowAtIndexPath 中执行此操作,同时使用旧的和新的 选择 indexPaths。

另见 Apple's guide了解更多详情。

关于ios - 点击时更改 UITableViewCell 高度时动画不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769780/

相关文章:

ios - Swift 中的 Int 到 UInt(反之亦然)位转换

iphone - 我怎样才能每 10 分钟使用一次重复间隔??

ios - 使用自定义 "dismiss modal"类动画弹出 UIViewController

swift - 是否可以更新 SKLabelNode?

ios - 无法连接 IBOutlet

ios - UITableViewCell 最小化和最大化动画

ios - 如何为所有 subview 设置文本颜色?

ios - 如何使用 Alamofire 在多部分表单数据中附加数组?

ios - Swift 3 使用对象映射器将字符串解析为数组返回 nil

ios - 应用程序试图在 didSelectRowAtIndexPath 中将一个 nil View Controller 推送到目标上