UIActivityIndicatorView Inside TableView Cell

2019-08-13 09:55发布

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];

2条回答
再贱就再见
2楼-- · 2019-08-13 10:30

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

[myspinner setCenter:cell.contentview.center];

this will work too.

查看更多
够拽才男人
3楼-- · 2019-08-13 10:38

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))];
查看更多
登录 后发表回答