无法修改自定义tableview单元属性

时间:2012-11-12 21:59:49

标签: ios uitableview

我尝试了所有内容,当我在didSelectRowForIndexPath中设置时,UIActivityMonitorView不会旋转。我使用

访问自定义tableview单元格

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

表格视图编程指南中查看Customizing Cells但是,故事板,NIB或以编程方式创建的UITableViewCell单元格之间的细节有所不同。您必须与我们分享您的tableView:cellForRowAtIndexPath:tableView:didSelectRowAtIndexPath:,以便诊断正在发生的事情。下面,我将向您展示一个使用旋转活动指示器的示例,但这里没有什么特别之处。我怀疑你的应用程序有一些简单的东西,但除非我们能看到你的代码,否则我们无法帮助你。

但是,为了向您展示一个示例,假设您已使用Interface Builder中定义的接口对UITableViewCell进行了子类化。以下示例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,故事板,以编程方式创建控件,应用程序模型的性质等)。与其尝试为您的应用程序改进上述代码,您可能更容易与我们分享您的代码,我们希望能够很快发现问题。问题可能很简单。