我得到了简单的表格 View ,而且每个单元格都有事件指示器,但是当我单击行按钮上的按钮时,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;
}
此代码部分有效,但事件指示器在所有行上启动动画 :) 我如何将此过程设置为仅 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 提供此 answer)
这又很简单,可以使用上面的代码。
在创建单元格时向按钮添加目标:
[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];
}
关于iOS 启动事件指示器只有 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28352702/