0

我已经尝试了所有方法,当我在 didSelectRowForIndexPath 中设置时 UIActivityMonitorView 不会旋转。我访问自定义表格视图单元格

CustomCell *cell = (CustomCell *) [tableview cellForRowAtIndexPath];

我可以看到在调试器中初始化的单元格及其属性,但是当我尝试修改单元格上的任何内容时,没有任何变化。我将单元格 activityMonitor IBOutlet 设置为在没有运气的 performSelectorAfterDelay 中开始旋转。当我尝试将单元格 alpha 设置为 0 时,也没有任何反应。

但是,如果我将微调器设置为通过 IB 旋转,它会在单元加载时工作。

我也尝试通过标签访问它,但它没有工作。

如何在自定义 tableview 单元格上设置属性?

4

1 回答 1

1

查看Table View Programming Guide中的自定义单元格。但是,故事板、NIB 或以编程方式创建的单元格之间的细节有所不同。您必须分享您的和您的,以便我们诊断正在发生的事情。下面,我将向您展示一个使用旋转活动指示器的示例,但这里没有什么特别之处。我怀疑您的应用程序发生了一些简单的事情,但除非我们能看到您的代码,否则我们无法为您提供帮助。UITableViewCelltableView:cellForRowAtIndexPath:tableView:didSelectRowAtIndexPath:

但是,为了向您展示一个示例,假设您已UITableViewCell使用 Interface Builder 中定义的接口进行子类化。didSelectRowAtIndexPath这是一个启动旋转活动指示器并在 15 秒后将其关闭的示例。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *cell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];

    // I have a model in my app for the sections of the tableview, as well as the rows,
    // so let's get the pointer to the appropriate model information. Your implementation
    // may differ, but hopefully you get the idea.

    Section *section = self.sections[indexPath.section];
    Row *row = section.rows[indexPath.row];

    // because we selected this row, start the activity indicator

    [cell addActivityIndicator];

    // let's flag the row in our model to indicate that we're busy here (so if and when
    // we represent this row, we'll know if we're busy or not)

    row.loading = YES;

    // and let's asynchronously dispatch a "stop activity indicator" in 15 seconds

    int64_t delayInSeconds = 15.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // because this is happening asynchronously, let's re-retrieve the cell
        // pointer, just in case it might have scrolled off of the visible screen
        // as is no longer visible or the pointer to the cell might have changed

        TestCell *currentCell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];

        // let's remove the activity indicator

        [currentCell removeActivityIndicator];

        // let's flag our model to indicate that this row is no longer loading

        row.loading = NO;
    });
}

在这种情况下,我需要确保tableView:cellForRowAtIndexPath:查看模型数据以确定它是否也需要显示旋转活动指示器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TestCell";
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // if I'm using storyboards, the following isn't needed, but if not, I might use something
    // like the following to load the custom cell from a NIB

    //if (cell == nil)
    //{
    //    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:self options:nil];
    //    cell = (TestCell *)[nib objectAtIndex:0];
    //}

    // this happens to be my model structure, where I have an array of sections,
    // and each section has its array of Row objects for that section

    Section *section = self.sections[indexPath.section];
    Row *row = section.rows[indexPath.row];

    // each "Row" object has two text fields, which I use to update the
    // two labels in my TestCell subclass of UITableViewCell

    cell.label1.text = row.text1;
    cell.label2.text = row.text2;

    // for this row of this section, figure out whether I need to start animating
    // the UIActivityIndicator

    if (row.loading)
        [cell addActivityIndicator];
    else
        [cell removeActivityIndicator];

    return cell;
}

在我的子类UITableViewCell中是添加和删除活动指示器的代码:

@interface TestCell ()

@property (strong, nonatomic) UIActivityIndicatorView *activity;

@end

@implementation TestCell

- (void)addActivityIndicator
{
    if (!self.activity)
    {
        self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.activity.center = self.contentView.center;
    }
    [self.contentView addSubview:self.activity];
    [self.activity startAnimating];
}

- (void)removeActivityIndicator
{
    if (self.activity)
    {
        [self.activity stopAnimating];
        [self.activity removeFromSuperview];
        self.activity = nil;
    }
}

@end

这只是它如何工作的一个简单示例,但实现可能会有很大差异(取决于 NIB、故事板、以编程方式创建控件、应用程序模型的性质等)。与其尝试为您的应用修改上述代码,不如与我们共享您的代码可能会更容易,我们希望能够很快发现问题。问题可能很简单。

于 2012-11-13T04:12:12.940 回答