我的自定义 tableview 单元格按钮单击事件有问题,所选单元格按钮上有加载事件指示器。 如果您有链接或其他来源,请帮助我。 我是 iOS 开发新手。
最佳答案
这要容易得多,因为它不涉及任何第三方的东西(尽管 MBProgressHUD 是一个很棒的工具)。当我创建单元格时,我创建了一个 UIACtivityIndicatorView 并将其添加为单元格的 accessoryView。稍后,当按下一行时,我会在适当的 indexPath 处获取对单元格本身的引用,然后访问其 accessoryView 属性,即指示器 View 。从那里你可以告诉它开始动画。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = activityIndicator;
}
cell.textLabel.text = _items[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect the row if you want the cell to fade out automatically after tapping
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get a reference to the cell that the user tapped
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// get the tapped cell's accessory view and cast it as the activity indicator view
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)cell.accessoryView;
// tell it to start animating
[activityIndicator startAnimating];
}
点击第一个单元格后会出现以下结果:
您必须根据要停止事件指示器旋转的时间/方式稍微更改代码,但如果您没有提供更多信息,这是我能提供的最佳信息。您可能希望将 indexPath.row 整数添加到 progressView 的标记属性,但还有更多内容。希望这对您有所帮助!
编辑
将标记添加到作为行的 indexPath 的按钮,并执行如下操作:
- (void)showProgressViewForButton:(id)sender {
NSInteger tappedCellIndex = sender.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tappedCellIndex inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)cell.accessoryView;
[activityIndicator startAnimating];
}
关于ios - 如何在自定义 TableviewCell 的按钮点击上添加 ActivityIndicator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21179218/