0

私はすべてを試しましたが、didSelectRowForIndexPath に設定したときに UIActivityMonitorView が回転しません。カスタムテーブルビューセルにアクセスします

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

デバッガーでセルとそのプロパティが初期化されているのを確認できますが、セルで何かを変更しようとしても何も変わりません。セルの activityMonitor IBOutlet を設定して、performSelectorAfterDelay で回転を開始しました。セルのアルファを 0 に設定しようとしても、何も起こりません。

ただし、スピナーをIB経由でスピンするように設定すると、セルがロードされたときに機能します。

また、タグ経由でアクセスしようとしましたが、うまくいきませんでした。

カスタム テーブルビュー セルのプロパティを設定するにはどうすればよいですか?

4

1 に答える 1

1

『テーブルビュープログラミングガイド』の「セルのカスタマイズ」を確認してください。ただし、詳細は、ストーリーボード、NIB、またはプログラムで作成されたセルによって異なります。あなたは私たちが何が起こっているのかを診断するためにあなたとあなたを共有しなければなりません。以下に、回転アクティビティインジケーターの使用例を示しますが、ここでは特別なことは何もありません。アプリで何か簡単なことが起こっているのではないかと思いますが、コードが表示されない限り、私たちがお手伝いできることはありません。UITableViewCelltableView:cellForRowAtIndexPath:tableView:didSelectRowAtIndexPath:

ただし、例を示すためにUITableViewCell、InterfaceBuilderで定義されたインターフェイスでサブクラス化したと仮定します。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 に答える