我正在制作一个作为 UITableView 的应用程序,它从 Web 获取内容、解析它并显示它。获取和解析它需要一些时间,所以我使用加载指示器 ( MBProgressHUD ) 并在后台进行加载。我想在 TableView 的底部添加一个按钮,所以我编写了这段代码:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(myAction)
forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Button Title" forState:UIControlStateNormal];
myButton.frame = CGRectMake(0, 0, 160.0, 40.0);
self.tableView.tableFooterView=myButton;
问题是它是在我的后台内容加载代码之后的 ViewDidLoad() 期间在 tableview 中初始化的
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading";
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
[self getContent];
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
所以当 View 加载时,按钮设置在顶部(因为 tableview 是空的)并且当它重新加载时,按钮保持在顶部,我需要转到另一个 View 并返回以在页脚中放置按钮.
有没有办法在内容加载后设置按钮?像一个 -(void)tableViewDidReloadData
函数?
谢谢!
最佳答案
是的,如果您在与 [self.tableView reloadData]
相同的调用中执行此操作(这是一个同步调用),它将在加载数据后显示。
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading";
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
[self getContent];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.tableView reloadData];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Button Title" forState:UIControlStateNormal];
[myButton setFrame:CGRectMake(0, 0, 160.0, 40.0)];
[self.tableView setTableFooterView:myButton];
});
});
关于objective-c - 表格重新加载后设置 UITableView FooterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10522638/