IPhone SDK:将UIActivityIndicatorView添加到UITableViewCell

IPhone SDK:将UIActivityIndicatorView添加到UITableViewCell,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,为什么单元格在此代码中不显示任何内容: UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [cell.imageView addSubview:spinner]; [spinner startAnimating]; cell.textLabel.text=@"Carregando.

为什么单元格在此代码中不显示任何内容:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=@"Carregando...";
[spinner release];
我在
tableView:cellforrowatinexpath:
中执行此操作

我尝试调整大小,为cell.imageView创建了一个框架,为微调器创建了一个相同大小的框架,但没有任何效果

这个代码有什么问题


谢谢你

我认为单元格的imageView可能会有一个零大小的矩形,因为您没有在其中放置图片。因此,微调器在内部,但不可见

因此,与其将微调器放在imageView中,不如将其放在单元格中

[cell addSubview:spinner];
spinner.frame = CGRectMake(145, 0, 30, 30); // if you want it centered horizontally...
你也可以这样做

cell.accessoryView = spinner;
< >把纺纱器放在电池的最右边。< /P> < P>我找到答案…

David Maymudes是对的。。。有必要为单元格设置“背景”。imageView。。。但必须是一个图像,而不仅仅是一个框架。因此,只需创建一个UIImage作为“白色背景”,并设置在cell.imageView.image中。守则如下:

 UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] 
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 UIImage *whiteback = [[UIImage alloc] initWithContentsOfFile:@"whiteback.png"];
 cell.imageView.image = whiteback;
 [cell.imageView addSubview:spinner];
 [spinner startAnimating];
 cell.textLabel.text=@"Carregando...";
 [whiteback release];
 [spinner release];
whiteback.png只是一个25x25像素的白色正方形


谢谢大家的帮助。。。再见…

对Thiago的方法稍作修改:

我认为将微调器直接添加到单元格视图感觉有点不舒服,因此我使用1x1透明png作为图像视图,并将其大小调整为我的微调器大小:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NoReuse"] autorelease];
cell.textLabel.text = @"Loading...";

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] 
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];

// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:@"spacer"];

UIGraphicsBeginImageContext(spinner.frame.size);

[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];

return cell;
这将生成一个如下所示的单元格:


您是否正在设置活动指示器的框架?类似于spinner.frame=CGRectMake(0,0,30,30)的东西?您还可以检查cell.imageView本身的框架和可见性。是的,我创建了两者,微调器框架和设置其可见性。。。但我找到了我描述的答案。。。谢谢你的帮助。。!acessoryView工作得很好。。。但是spinner.frame不。。。我找到了一个解决方案,就是创建一个“白色正方形”UIImage,并将它们作为cell.imageView.image。。。他们的旋转器出现了。。。但是谢谢你的帮助。。!对不起;我应该亲自测试,而不是相信其他评论者!