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 at activityIN
var as at convenience initializer of UIActivityIndicatorView
. If you only need activity for ten seconds, edit your func like this:
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
let weakActivityIndicator = activityIN
weakActivityIndicator.color = UIColor.greenColor()
weakActivityIndicator.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(weakActivityIndicator)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
weakActivityIndicator.stopAnimating()
weakActivityIndicator.hidden = true
}
}
}
But usually you need to keep reference to instance that you get from this initializer. Define UIActivityIndicatorView
stored property in your TransactionCell
class.
Assume that TransactionCell
class have declaration
var cellActivityIndicator: UIActivityIndicatorView!
then you can edit your func like this
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
cell.cellActivityIndicator = activityIN
cell.cellActivityIndicator.color = UIColor.greenColor()
cell.cellActivityIndicator.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(cell.cellActivityIndicator)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
cell.cellActivityIndicator.stopAnimating()
cell.cellActivityIndicator.hidden = true
}
}
}