iOS启动活动指示器仅1行



我得到了简单的表视图,每个单元格都有活动指示符,但当我点击行按钮时,alpha得到0,指示符alpha得到1,就像这个一样

if (webserviceRunningValueInt == 1)
{
    cell.unBlockUserButtonOutlet.alpha = 0;
    cell.waitIndicator.alpha = 1;
    [cell.waitIndicator startAnimating];
}
else
{
     cell.waitIndicator.alpha = 0;
     [cell.waitIndicator stopAnimating];
     cell.unBlockUserButtonOutlet.alpha = 1;
}

这个代码部分工作,但活动指示器在所有行上启动动画:)我怎么能只设置一行这个过程

希望我能正确理解你的问题:你想点击一个单元格并让一个活动指示器开始动画?

我个人不会在所有的指示器上都有活动指示器,然后隐藏和显示它们,这会让启动和停止正确的指示器有点痛苦。

我在其他应用程序中的做法是在didSelectRow阶段向单元格添加一个指示符,因为我们可以知道我们点击了哪个单元格,然后立即添加一个单元格:

- (void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView_ reloadData];
    // This code will create and add an activity indicator
    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UITableViewCell * cell = [tableView_ cellForRowAtIndexPath:indexPath];
    cell.accessoryView = activityIndicator;
    [activityIndicator startAnimating];
    [tableView_ deselectRowAtIndexPath:indexPath animated:YES];
}

这段代码的巧妙之处在于,由于我们在创建单元格时不添加活动指示符,这意味着我们可以通过刷新tableView来消除指示符。在这种情况下,我们每次点击都会去掉以前的指示器,并添加一个新的指示器。

希望这对有所帮助

编辑:如果你想通过按下单元格中的按钮来实现这一点(感谢Vladimir的回答)

这也很简单,可以使用上面的代码。

创建单元格时向按钮添加目标:

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

这将使我们能够了解何时点击按钮

- (void)checkButtonTapped:(id)sender {
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    // Now we have the indexPath of the cell clicked so we can use the previous code again
    [tableView_ reloadData];
    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UITableViewCell * cell = [tableView_ cellForRowAtIndexPath:indexPath];
    cell.accessoryView = activityIndicator;
    [activityIndicator startAnimating];    
}

最新更新