IPhone SDK:将UIActivityIndi​​catorView添加到UITableViewCell

Thi*_*ago 8 iphone cocoa-touch uitableview

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

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=@"Carregando...";
[spinner release];
Run Code Online (Sandbox Code Playgroud)

我在里面做这个tableView:cellForRowAtIndexPath:.

我尝试了尺寸以适应,cell.imageView为旋转器创建一个框架和相同大小的框架,但没有任何作用.

这段代码有什么问题?

谢谢..!

Thi*_*ago 9

我找到了答案......

David Maymudes是偏好正确的......有必要为cell.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];
Run Code Online (Sandbox Code Playgroud)

whiteback.png只是一个25x25像素的白色方块......

谢谢大家的帮助......见到你......


Chr*_*s R 9

从Thiago稍微修改一下这个方法:

我认为将微调器直接添加到单元格视图感觉有点hacky,所以我使用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;
Run Code Online (Sandbox Code Playgroud)

这给出了一个如下所示的单元格:

加载细胞


Dav*_*des 3

我认为单元格的 imageView 可能会有一个零大小的矩形,因为你还没有在其中放入图片。所以旋转器在里面但不可见。

因此,不要将旋转器放在 imageView 中,而是将其放在单元格中......

[cell addSubview:spinner];
spinner.frame = CGRectMake(145, 0, 30, 30); // if you want it centered horizontally...
Run Code Online (Sandbox Code Playgroud)

你也可以这样做

cell.accessoryView = spinner;
Run Code Online (Sandbox Code Playgroud)

将旋转器放在单元格的最右侧。