【问题标题】:Animate table view cells including header动画表格视图单元格,包括标题
【发布时间】:2016-02-23 15:57:57
【问题描述】:

我使用以下方法为表格视图的单元格设置动画:

func animateCells(tableView: UITableView) {
    tableView.reloadData()

    let cells = tableView.visibleCells
    let tableHeight: CGFloat = tableView.bounds.size.height

    for i in cells {
        let cell: UITableViewCell = i as UITableViewCell
        cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
    }

    var index = 0

    for a in cells {
        let cell: UITableViewCell = a as UITableViewCell
        UIView.animateWithDuration(animationDuration, delay: animationDelay * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            cell.transform = CGAffineTransformMakeTranslation(0, 0);
            }, completion: nil)

        index += 1
    }
}

这仅适用于表格视图中的单元格。但我也有一些带有标题的表格,在这些情况下动画不起作用。如何在上面的动画代码中包含标题?谢谢。

【问题讨论】:

  • 我认为您必须使用tableView.headerViewForSection()tableView.tableHeaderView 而不是visibleCells 才能获得标题视图。

标签: ios swift uitableview


【解决方案1】:

解决了 tableView 部分标题动画的问题:

// animate section header
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let numberOfSections = tableView.numberOfSections
    let tableHeight: CGFloat = tableView.bounds.size.height
    var index = 0

    for i in 0...numberOfSections - 1 {
        if (section == i) {
            view.transform = CGAffineTransformMakeTranslation(0, tableHeight)
            UIView.animateWithDuration(1.0, delay: 0.05 * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                view.transform = CGAffineTransformMakeTranslation(0, 0)
                }, completion: nil)
        }
        index += 1
    }
}

对于细胞:

// animate cells
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let numberOfSections = tableView.numberOfSections
    let tableHeight: CGFloat = tableView.bounds.size.height
    var index = 0

    for i in 0...numberOfSections - 1 {
        let numberOfCells = tableView.numberOfRowsInSection(i)

        for j in 0...numberOfCells - 1 {
            if (indexPath.section == i && indexPath.row == j) {
                cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
                UIView.animateWithDuration(1.0, delay: 0.05 * Double(index),usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
                    cell.transform = CGAffineTransformMakeTranslation(0, 0);
                    }, completion: nil)
            }
            index += 1
        }
    }
}

谢谢你的提示!!

【讨论】:

    【解决方案2】:

    UITableView 中有两个标题视图。

    UITableView 的头部视图

    这是显示在UITableView 上方的附件视图。可以这样设置:

    self.tableView.tableHeaderView = UIView()
    

    您也可以从tableHeaderView 属性中获取标题视图。

    部分的标题视图

    每个部分都可以有自己的标题视图。为了设置部分tableView(_:viewForHeaderInSection:) 的标题视图UITableViewDelegate 必须实现。

    您可以从UITableViewheaderViewForSection(_:) 获取部分的标题视图。

    let headerView = self.tableView.headerViewForSection(0)!
    

    方法tableView(_:viewForHeaderInSection:)根据the reference document返回UIView,但在你的实现中它必须返回UITableViewHeaderFooterView对象。如果返回UIView,则headerViewForSection(_:) 将返回nil

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UITableViewHeaderFooterView()
        headerView.backgroundColor = UIColor.redColor()
    
        return headerView
    }
    

    我希望能解决你的问题。

    【讨论】:

    • 使用tableView.visibleCells 我可以访问表格视图的所有单元格。是否有类似的方法来访问表格视图的所有部分/部分标题?
    • headerViewForSection(_:) 似乎是你想要的。
    • 这是访问索引 XY 处的一个部分..但是我如何访问我的 tableView..类似于 tableView.visibleCells 的所有部分?
    • 如果要访问所有节标题,请使用 for-loop。 for i in 0...((self.tableView?.numberOfSections)! - 1) { let headerView = self.tableView?.headerViewForSection(i) // ... }
    • 我不知道其他方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2021-11-18
    • 2019-09-16
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多