问题描述
我有一个包含 UIActivityIndicatorView(微调器)的自定义 UITableViewCell,我尝试单击单元格,以便微调器开始动画.所以我尝试在 UITableViewController 中实现以下内容:
I have a custom UITableViewCell containing a UIActivityIndicatorView (spinner), and I try to click on the cell so that spinner starting to animate. So I try to implement following in UITableViewController:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("testcase", forIndexPath: indexPath) as TestCaseTableViewCell
cell.spinner.startAnimating()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
我的 TestCaseTableViewCell(自定义单元格类)中有实例变量spinner":
I have the instance variable "spinner" in my TestCaseTableViewCell(custom cell class):
@IBOutlet weak var spinner: UIActivityIndicatorView!
但是没有用......
But it didn't work......
我只想点击单元格,微调器开始动画,因为我想在这段时间内做一些事情.完成某些操作后,我可以在单元格中显示类似OK"的内容(与微调器的位置相同).我怎样才能做到这一点?
I just want to click on the cell, and the spinner starts to animate cause I want to do something in this period. While the something is done, I can show something like "OK" in the cell(as the same position of the spinner). How can I achieve that?
推荐答案
问题在于如何从表格视图中检索单元格:dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath)
.当您需要显示新单元格时,此方法会从其重用缓存中向 UITableView
询问单元格,因此只能在 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
表视图数据源的方法.
The problem is with how you are retrieving your cell from the table view: dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath)
. This method asks the UITableView
for a cell from its reuse cache when you need a new cell to display, so should only be used in the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
method of your table view's data source.
要向表格视图询问屏幕单元格,请使用cellForRowAtIndexPath(indexPath: NSIndexPath)
.您的代码示例将变为:
To ask the table view for an on-screen cell, use cellForRowAtIndexPath(indexPath: NSIndexPath)
. Your code sample then becomes:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? TestCaseTableViewCell {
cell.spinner.startAnimating()
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
这篇关于UITableViewCell 中的 UIActivityIndicatorView(微调器)无法在 swift 中通过 UITableViewController 开始动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!