1

我正在制作一个作为 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];
        });
    }); 

因此,当视图加载时,按钮设置在顶部(因为 tableview 是空的),当它重新加载时,按钮保持在顶部,我需要转到另一个视图并返回页脚中的按钮。

有没有办法在内容加载后设置按钮?像一个-(void)tableViewDidReloadData函数?

谢谢 !

4

3 回答 3

1

是的,如果您在与[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];
    });
});
于 2012-05-09T19:19:42.147 回答
0

您应该[self.tableView reloadData];只在主线程中调用。

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
于 2012-05-09T19:24:00.387 回答
0

不要创建 myButton 并将其分配给 ViewDidLoad 委托中的 tableview 页脚,而不是在隐藏 MBProgress HUD 并重新加载表格时创建它

试试这个它应该工作

dispatch_async(dispatch_get_main_queue(), ^{
        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;
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
于 2012-05-10T17:11:19.877 回答