【问题标题】:How to add ActivityIndicator on Custom TableviewCell's button click?如何在自定义 TableviewCell 的按钮单击上添加 ActivityIndi​​cator?
【发布时间】:2014-01-17 06:23:32
【问题描述】:

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

【问题讨论】:

  • 为此使用了 MBProgessbar。
  • 如果您有任何链接,请分享。
  • 检查这个:github.com/jdg/MBProgressHUD 如果有任何疑问,请告诉我。
  • 感谢您的回复..
  • 我想在 tableview 的每个自定义单元格上添加该指示器,当我选择其中一个时,该指示器将显示在该单元格上。

标签: ios uitableview uiactivityindicatorview


【解决方案1】:

这要容易得多,因为它不涉及任何第三方的东西(尽管 MBProgressHUD 是一个很棒的工具)。创建单元格时,我创建了一个 UIACtivityIndi​​catorView 并将其添加为单元格的附件视图。稍后,当按下一行时,我在适当的 indexPath 处获取对单元格本身的引用,然后访问其附件视图属性,即指示器视图。从那里你可以告诉它开始动画。

- (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 的 tag 属性,但还有更多内容。希望这会有所帮助!

编辑

将标签添加到作为该行的 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];
}

【讨论】:

  • 感谢您的回答.. 它正在工作.. 但我在自定义单元格中使用了按钮,所以点击它;该行的指示器将显示。有什么想法请给我建议。
【解决方案2】:

这是 MBProgressHUD 的链接:

https://github.com/jdg/MBProgressHUD

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多