Iphone 如何在UITableView的节标题视图中访问UIActivityIndicatorView?
我想用我的UITableView做一些非常简单的事情:我想将UIActivityIndicatorView添加到一个节的标题视图中,并让它在我想要的时候动画化或消失 我使用tableView:viewForHeaderInSection将UIActivityIndicatorView添加到标题视图时没有遇到任何问题:Iphone 如何在UITableView的节标题视图中访问UIActivityIndicatorView?,iphone,uitableview,Iphone,Uitableview,我想用我的UITableView做一些非常简单的事情:我想将UIActivityIndicatorView添加到一个节的标题视图中,并让它在我想要的时候动画化或消失 我使用tableView:viewForHeaderInSection将UIActivityIndicatorView添加到标题视图时没有遇到任何问题: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- (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];
}
activityIndicator是控制器类的属性。
我在viewDidLoad方法中分配它:
- (void)viewDidLoad
{
(...)
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(200, 10, 25, 25)];
}
通过这种方式,我可以随时向它发送消息,如启动动画或停止动画。
问题是,当我滚动tableView时,activityIndicator就会消失,我想这是因为tableView:ViewForHeaderSection:方法被第二次调用
我还可以如何将activityIndicatorView添加到节的标题视图中,然后仍然能够向其发送消息?当然,当我向下滚动时,activityIndicator不会消失
多谢各位 如果你试图在多个地方使用同一个活动指示器,那么它可能会从一个地方移动到另一个地方。我相信你需要一个不同的每一节标题。您可能希望使用一个可变数组来跟踪您创建的标题视图,这样,如果在数组中发现一个没有超级视图,您就可以重用它们,有点类似于取消队列和重用单元格
这只是一个猜测,因为我还没有这样做,但我很确定问题在于试图在多个位置重用同一视图。如果您尝试在多个位置使用同一活动指示器,那么它可能会从一个位置移动到另一个位置。我相信你需要一个不同的每一节标题。您可能希望使用一个可变数组来跟踪您创建的标题视图,这样,如果在数组中发现一个没有超级视图,您就可以重用它们,有点类似于取消队列和重用单元格
这只是一个猜测,因为我没有这样做,但我很确定问题是试图在多个位置重用同一个视图。问题似乎是由于每次调用tableView:ViewForHeaderSection:时都重新创建customView并将activityIndicator添加为子视图而引起的 不使用子视图帮助我修复了它:
- (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];
}
虽然看起来很难看,但activityIndicator占据了整个区域的宽度。我最好为第1节创建一个唯一的customView,并一次性将activityIndicator添加为子视图。问题似乎是由于每次调用tableView:viewForHeaderInSection:时都重新创建customView并将activityIndicator添加为子视图造成的 不使用子视图帮助我修复了它:
- (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];
}
虽然看起来很难看,但activityIndicator占据了整个区域的宽度。我最好为第1节创建一个唯一的customView,并一次性将activityIndicator添加为子视图。我不希望activityIndicator出现在多个位置,只出现在一个位置。嗯,这就是你想要做的,因为我无法想象有任何其他理由继续将子视图添加到same中。我不想在多个位置上有一个activityIndicator,就在一个位置。好吧,这就是你想要做的,因为我无法想象有任何其他理由继续将子视图添加到same中