2010-01-13 43 views
2

我想用我的UITableView做一些非常簡單的事情:我想添加一個UIActivityIndi​​catorView到節的標題視圖,並使其動畫或消失,只要我想。如何在UITableView的節標題視圖中訪問UIActivityIndi​​catorView?

我沒有任何麻煩,添加UIActivityIndi​​catorView使用的tableView頭視圖:viewForHeaderInSection:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)]; 

// create the title 
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)]; 
headerLabel.text = @"some random title here"; 

[customView addSubview:headerLabel]; 
[headerLabel release]; 

// Add a UIActivityIndicatorView in section 1 
if(section == 1) 
{ 
    [activityIndicator startAnimating]; 
    [customView addSubview:activityIndicator]; 
} 

return [customView autorelease]; 

}

activityIndi​​cator是我的控制器類的屬性。 我ALLOC它在viewDidLoad方法:

- (void)viewDidLoad 
{ 
(...) 
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(200, 10, 25, 25)]; 
} 

這樣我可以發送消息給它(如-startAnimating或-stopAnimating)每當我想要的。 問題是activityIndi​​cator一旦我滾動tableView就消失了(我想這是因爲tableView:viewForHeaderInSection:方法被第二次調用)。

還有什麼可以將activityIndi​​catorView添加到該部分的標題視圖,並且仍然可以向其發送消息? (當然,當我向下滾動時activityIndi​​cator不會消失)

非常感謝!

回答

0

如果您嘗試在多個地方使用相同的活動指示符,那麼它可能會從一個地方移動到另一個地方。我相信你需要爲每個單獨的部分標題添加一個不同的標題。您可能希望使用MutableArray來跟蹤您創建的標題視圖,以便在陣列中找不到超級視圖時使用它們,有點像出列和重用單元格。

這只是一個猜測,因爲我沒有這樣做,但我敢肯定這個問題試圖在多個地方重複使用相同的視圖。

+0

我不想在多個地方有一個activityIndi​​cator,只有一個。 – nmondollot 2010-01-14 10:07:05

+0

好吧,這就是它看起來像你試圖做的,因爲我無法想象任何其他原因繼續添加子視圖相同 – Nimrod 2010-01-14 16:39:56

0

該問題似乎是由於每次調用tableView:viewForHeaderInSection:時重新創建customView並將activityIndi​​cator添加爲子視圖引起的。

不使用子視圖幫我解決這個問題:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

// Add a UIActivityIndicatorView in section 1 
if(section == 1) 
{ 
    [activityIndicator startAnimating]; 
    return activityIndicator; 
} 

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)]; 

// create the title 
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)]; 
headerLabel.text = @"some random title here"; 

[customView addSubview:headerLabel]; 
[headerLabel release]; 


return [customView autorelease]; 
} 

(它看起來很醜陋雖然,activityIndi​​cator取部分的整個寬度,我最好的第1節創造一個獨特的customView並添加。 activityIndi​​cator作爲子視圖一勞永逸)。

相關問題