swift - 在 Swift 2.0 的 UITableView 中动态移动事件指示器

标签 swift uitableview ios9 uiactivityindicatorview

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

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

最佳答案

我认为您可以使用 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)
}

关于swift - 在 Swift 2.0 的 UITableView 中动态移动事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881888/

相关文章:

iphone - 可滚动且与 UITable 滚动同步的背景图像

ios9 TouchID 检测是否添加或删除了指纹

ios - 通过 WatchConnectivity 发送数据时有效载荷有多大?

ios - Xcode 与模拟器 iOS 9 失去连接

ios - 我如何处理导航后退按钮 View 层次结构?

ios - [String] 常量发生内存泄漏

ios - 在编辑模式下使用不同的表格 View 单元格

ios - 使用仅适用于 iOS 7 的应用程序处理 Core Data 和 iCloud 的正确方法

ios - 如何从 SwiftUI View 返回到 UIKit?

ios - 如何切换 UITableViewContoller/UITableViewCell 中的选定行