TableView 单元格内的 UIActivityIndicatorView
UIActivityIndicatorView Inside TableView Cell
我正在尝试将 Spinner 添加到我在 tableViewCell 中放置的 Like 按钮。但问题是微调器显示在 tableViewCell 之外。
这就是我在 cellForRowAtIndexPath
中实现代码的方式
myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter:cell.like.center];
[cell.like addSubview:myspinner];
当我点击使用 sender.tag
[myspinner startAnimating];
问题是微调器工作但不是我想要的。它显示在单元格外。
更新
matt
的回答确实有效。
我也像下面这样更改了我的代码。内部选择器动作。 -(void) likeClicked:(UIButton*)sender
UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter: CGPointMake(CGRectGetMidX(sender.bounds),
CGRectGetMidY(sender.bounds))];
[sender addSubview:myspinner];
[myspinner startAnimating];
这种形式的代码:
[myspinner setCenter:cell.like.center];
...永远不会对。原因是 myspinner.center
在其父视图的坐标中——也就是说,在 cell.like
坐标中。但是 cell.like.center
在 它的 超级视图的坐标中。因此,您是在比较苹果和橙子:您是根据位于完全不同坐标系中的另一个点来设置一个点。那只能偶然起作用。
您要做的是将 myspinner.center
设置到其父视图的 边界 的中心。这些值在相同的坐标系中(这里是cell.like
的坐标系)。
[myspinner setCenter: CGPointMake(CGRectGetMidX(cell.like.bounds),
CGRectGetMidY(cell.like.bounds))];
或者您可能想使用单元格的内容视图来获取单元格的中心,
[myspinner setCenter:cell.contentview.center];
这也行。
我正在尝试将 Spinner 添加到我在 tableViewCell 中放置的 Like 按钮。但问题是微调器显示在 tableViewCell 之外。
这就是我在 cellForRowAtIndexPath
myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter:cell.like.center];
[cell.like addSubview:myspinner];
当我点击使用 sender.tag
[myspinner startAnimating];
问题是微调器工作但不是我想要的。它显示在单元格外。
更新
matt
的回答确实有效。
我也像下面这样更改了我的代码。内部选择器动作。 -(void) likeClicked:(UIButton*)sender
UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter: CGPointMake(CGRectGetMidX(sender.bounds),
CGRectGetMidY(sender.bounds))];
[sender addSubview:myspinner];
[myspinner startAnimating];
这种形式的代码:
[myspinner setCenter:cell.like.center];
...永远不会对。原因是 myspinner.center
在其父视图的坐标中——也就是说,在 cell.like
坐标中。但是 cell.like.center
在 它的 超级视图的坐标中。因此,您是在比较苹果和橙子:您是根据位于完全不同坐标系中的另一个点来设置一个点。那只能偶然起作用。
您要做的是将 myspinner.center
设置到其父视图的 边界 的中心。这些值在相同的坐标系中(这里是cell.like
的坐标系)。
[myspinner setCenter: CGPointMake(CGRectGetMidX(cell.like.bounds),
CGRectGetMidY(cell.like.bounds))];
或者您可能想使用单元格的内容视图来获取单元格的中心,
[myspinner setCenter:cell.contentview.center];
这也行。