2010-01-13 60 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作为子视图一劳永逸)。

相关问题