『テーブルビュープログラミングガイド』の「セルのカスタマイズ」を確認してください。ただし、詳細は、ストーリーボード、NIB、またはプログラムで作成されたセルによって異なります。あなたは私たちが何が起こっているのかを診断するためにあなたとあなたを共有しなければなりません。以下に、回転アクティビティインジケーターの使用例を示しますが、ここでは特別なことは何もありません。アプリで何か簡単なことが起こっているのではないかと思いますが、コードが表示されない限り、私たちがお手伝いできることはありません。UITableViewCell
tableView: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、ストーリーボード、プログラムによるコントロールの作成、アプリのモデルの性質などによって異なります)。上記のコードをアプリに後付けするよりも、コードを私たちと共有する方がおそらく簡単であり、問題をかなり迅速に発見できることを願っています。問題はおそらく単純なものです。