ios - 如何在自定义 TableviewCell 的按钮点击上添加 ActivityIndi​​cator?

标签 ios uitableview uiactivityindicatorview

我的自定义 tableview 单元格按钮单击事件有问题,所选单元格按钮上有加载事件指示器。 如果您有链接或其他来源,请帮助我。 我是 iOS 开发新手。

最佳答案

这要容易得多,因为它不涉及任何第三方的东西(尽管 MBProgressHUD 是一个很棒的工具)。当我创建单元格时,我创建了一个 UIACtivityIndi​​catorView 并将其添加为单元格的 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];
}

点击第一个单元格后会出现以下结果:

enter image description here

您必须根据要停止事件指示器旋转的时间/方式稍微更改代码,但如果您没有提供更多信息,这是我能提供的最佳信息。您可能希望将 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 的按钮点击上添加 ActivityIndi​​cator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21179218/

相关文章:

ios - 如何提取文本中的电子邮件/邮箱字符串列表或测试字符串是否为正确的电子邮件地址?

ios - 是否可以同时在 react-native 中使用前置和后置摄像头?

objective-c - 数据上传期间在 UITableView 上的 UIProgressView+Activity Indicator View

iPhone:更改 CATransition 会导致 UIActivityIndi​​catorView 停止动画

ios - UITableViewController 中的 didSelectRowAtIndexPath 不起作用

iphone - UIActivityIndi​​cator 问题——有时它不会停止

android - 无法在 ios 和 android 之间创建应答 sdp (mediatek)

ios - 如何让xcode 4.6中的场景停靠栏显示 View Controller 名称?

ios - (iOS7) Scroll to cursor in UITextView in UITableView in UIScrollView

ios - UITableView - 如何在 + 和 - 单击时更新数组?