I have declared a UIActivityIndicatorView as a computed property in extension of my model. I have a method configureCell where I'm trying to use activity indicator as a subview of an imageView. Here I'm able to position the indicator on certain condition but am not able to do any changes on it later. E.g I am not able to stop the activity indicator, not able to change the color, not able to even hide it.
extension TranscationModel: UITableViewDataSource, UITableViewDelegate
{
var activityIN: UIActivityIndicatorView {
var act = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
act.color = UIColor.redColor()
act.hidden = false
act.startAnimating()
return act
}
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
activityIN.color = UIColor.greenColor() //Even this doesn't work
activityIN.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(activityIN)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
activityIN.stopAnimating() //Not working
activityIN.hidden = true //Not working
}
}
}
You defined
activityIN
as computed variable. Any time when you call it, you get completely new instance. Look atactivityIN
var as at convenience initializer ofUIActivityIndicatorView
. If you only need activity for ten seconds, edit your func like this:But usually you need to keep reference to instance that you get from this initializer. Define
UIActivityIndicatorView
stored property in yourTransactionCell
class.Assume that
TransactionCell
class have declarationthen you can edit your func like this