【问题标题】:Move activity indicator dynamically in UITableView in Swift 2.0在 Swift 2.0 的 UITableView 中动态移动活动指示器
【发布时间】:2015-10-01 07:34:14
【问题描述】:

我知道将 activityIndi​​cator 添加到视图中的某个位置,在需要时启动和停止动画。但是在我的应用程序中的某些情况下,当用户单击单元格(带有标题和副标题)时,我会在后台获取一些数据,然后向用户显示警报以获取一些输入。在所有这些情况下,我只是在中间显示一个活动指示器,这看起来不太好。因此,我想仅在用户单击的单元格末尾显示活动指示器。有没有办法做到这一点?

我能想到的一件事是使用自定义表格 View Cell,然后在此处显式添加活动指示器。但是是否可以对标准标题和副标题单元格执行相同的操作,并且可以根据用户单击的单元格的位置动态重新定位活动指示器?

【问题讨论】:

    标签: swift uitableview ios9 uiactivityindicatorview


    【解决方案1】:

    我认为你可以使用 UITableViewCell 的 accessoryView 来做到这一点

    var selectedIndexPath : NSIndexPath?
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        // ...
    
        if (indexPath == selectedIndexPath)
        {
            cell.accessoryView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
            (cell.accessoryView as! UIActivityIndicatorView).startAnimating()
        }
        else
        {
            cell.accessoryView = nil
        }
    
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        selectedIndexPath = indexPath
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
    

    【讨论】:

    • 它确实有效。我所做的唯一更改是在调用 didSelectRowAtIndexPath 时获取单元格: )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多