2009-08-12 81 views
8

爲什麼不顯示細胞在這段代碼什麼:IPhone SDK:添加UIActivityIndi​​catorView到一個UITableViewCell

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[cell.imageView addSubview:spinner]; 
[spinner startAnimating]; 
[email protected]"Carregando..."; 
[spinner release]; 

我做這裏面tableView:cellForRowAtIndexPath:

我試過適合的大小,創建一個框架到cell.imageView和一個相同大小的框架的微調,但沒有任何作品。

這段代碼有什麼問題?

謝謝..!

+0

你設置活動的指標的框架?像spinner.frame = CGRectMake(0,0,30,30)?你也可以檢查cell.imageView本身的框架和可見性。 – Tyler 2009-08-12 23:22:06

+0

是的,我創造了兩個,微調框架和設置他的知名度......但我找到了答案,因爲我在下面描述...謝謝你的幫助..! – Thiago 2009-08-13 03:49:58

回答

3

我認爲單元格的imageView可能會有一個零大小的矩形,因爲您沒有在其中放置圖片。所以紡紗工在裏面,但看不見。的

因此,而不是把微調的ImageView的範圍內,只要把它在細胞內...

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

你也可以做

cell.accessoryView = spinner; 

把微調在最右側的細胞。

+0

acessoryView工作得很好......但spinner.frame不...我找到了一個解決方案是創建一個「白色正方形」的UIImage和他們把cell.imageView.image ...他們的微調出現...但感謝您的幫助..! – Thiago 2009-08-13 03:38:46

+0

對不起,我應該自己測試一下,而不是相信其他評論者! – 2009-08-13 15:42:00

9

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

大衛Maymudes是partialy吧... It's必須有「背景」的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]; 
[email protected]"Carregando..."; 
[whiteback release]; 
[spinner release]; 

的whiteback.png僅僅是一個25×25像素的白色方形...

感謝大家幫助......看你......

9

稍加改進從蒂亞戈方法:

我想直接添加微調到細胞視圖感到有點哈克,所以就用一個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; 

這讓看起來像這樣的小區:

loading cell

+0

就像一個魅力:-) – crosscode 2012-01-10 23:10:05

相關問題