2017-10-21 66 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. 

希望這將工作!