2017-10-21 70 views
0

我想在每个UITableViewCell内部都有ActivityIndicatorView,并在点按后开始动画(以显示动作正在进行)。但我无法启动动画效果。iOS:无法使用UITableViewCell内部的活动指示器

我将ActivityIndicatorView添加到我的电池原型并通过@IBOutlet连接。这是我的代码用户选择表行后启动动画:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let selectedAction = actions[indexPath.row] 

     let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionTableViewCell 

     cell.activityInProgressIndicator.startAnimating() 

    } 

我也尝试添加的ActivityIndicatorView新的实例作为cell.accessoryView。没有任何影响。

我也试图更新单元格或者通过tableView.reloadData()(我想避免的)和tableView.beginUpdates()tableView.endUpdates()

理论上讲,该纺纱指标应该被隐藏,但didSelectRowAt设置.isHidden = false也不起作用。

+0

你能看到那里的指标吗?问题只是为了启动它? –

+0

@MilanNosáľ是的,如果我没有设置isHidden为true,那么指标是可见的。 – Filip

回答

2

问题是,当您在didSelectRowAt内使用tableView.dequeueReusableCell时,您正在创建ActionTableViewCell的新实例,这是错误的。 您需要使用这样的事情:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    guard let cell = tableView.cellForRow(at: indexPath) as? ActionTableViewCell else { return } 
    cell.activityInProgressIndicator.startAnimating() 
} 

这将解决您的问题。

3

只需更换

let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", 
for: indexPath) as! ActionTableViewCell with below line 

let cell = tableView.cellForRow(at: indexPath) as! ActionTableViewCell. 

希望这将工作!