在 Swift 2.0 的 UITableView 中动态移动 activity 指示器

Move activity indicator dynamically in UITableView in Swift 2.0

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

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

我想你可以使用 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)
}