Im Batman
Im Batman

Reputation: 1876

UIActivityIndicatorView Inside TableView Cell

I'm trying to add Spinner to Like button that I have positioned in my tableViewCell. But the problem is spinner is showing outside the tableViewCell.

This is how I implemeneted my code inside cellForRowAtIndexPath

myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

[myspinner setCenter:cell.like.center];
[cell.like addSubview:myspinner];

And when I click using sender.tag

[myspinner startAnimating];

Problem is spinner working but not as I wanted. It's showing outside the cell.

UPDATE

With matt's answer It did work. also i changed my code like below. inside selector action. -(void) likeClicked:(UIButton*)sender

UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [myspinner setCenter: CGPointMake(CGRectGetMidX(sender.bounds),
                                      CGRectGetMidY(sender.bounds))];
    [sender addSubview:myspinner];
    [myspinner startAnimating];

Upvotes: 1

Views: 194

Answers (2)

Devang Tandel
Devang Tandel

Reputation: 3008

or you may want to use cell's content view to get centre of cell,

[myspinner setCenter:cell.contentview.center];

this will work too.

Upvotes: 0

matt
matt

Reputation: 534893

Code of this form:

[myspinner setCenter:cell.like.center];

...is never right. The reason is that myspinner.center is in its superview's coordinates — that is, in cell.like coordinates. But cell.like.center is in its superview's coordinates. Thus, you are comparing apples with oranges: you are setting a point in terms of another point that lives in a completely different coordinate system. That can only work by accident.

What you want to do is set myspinner.center to the center of its superview's bounds. Those values are in the same coordinate system (here, cell.like's coordinate system).

[myspinner setCenter: CGPointMake(CGRectGetMidX(cell.like.bounds),
                             CGRectGetMidY(cell.like.bounds))];

Upvotes: 2

Related Questions