我有一个带有应用程序的健身计划页面,该应用程序是一个带有多个自定义uitableviewcells的UITableViewController。它依赖于可靠的大型数据源 - 我希望在访问此页面时显示加载程序,同时从服务器撤回数据源。
我已经设置了一个包含样式化加载器消息/活动指示符的自定义uiTableviewCell,并希望在加载时显示此信息 - 然后在数据可用时 - 刷新tableview并将数据转发到相关单元格中。
目前我的viewdidload方法中有以下方法,如果Feed没有完成其加载,该方法当前会显示警告 -
[[RKObjectManager sharedManager].HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if(status == AFNetworkReachabilityStatusNotReachable)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message:@"There is no network connection!"
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}
else
{
我希望改变它以显示加载器单元 - 然后在数据加载完成后刷新视图 -
我改变了以下内容 -
[[RKObjectManager sharedManager].HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if(status == AFNetworkReachabilityStatusNotReachable)
{
_WoHpTV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
workoutBannerCell *cell = [_WoHpTV
dequeueReusableCellWithIdentifier:@"loaderCell" ];
}
else
{
所以我已经参考了上面的自定义单元格 - 但我的问题是如何将它添加到我的TableView中?
答案 0 :(得分:3)
我已经参考了上面的自定义单元 - 但我的问题 是如何将其添加到我的TableView?
您需要实现UITableView
的数据源方法。由于您希望在每个表格单元格中显示加载指示符,因此您需要一个临时数据(您无法在表格视图中加载0个单元格,即使显示加载程序,您也需要一些可见单元格)。在视图中,load创建了一个临时对象数组并调用tableview的reloadData
。
for(int i = 0; i < 5; i++) {
[_dataArray addObject:@{@"text":@"text_value"}];
_isDataLoaded = NO;
[_table reloadData];
现在,这将使用Temp数据填充您的表。完成HTTP调用后,将BOOL isDataLoaded
设置为YES
然后在数据源方法 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!_isDataLoaded)
return _dataArray.count; // No data return count for Temp Data
else
return _feedArray.count; // Return correct feed items count
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell_id"];
if (cell == nil)
// Initialise cell here
if (!_isDataLoaded) { // Data has not yet loaded. Set loaders
NSDictionary *data = [_dataArray objectAtIndex:indexPath.row];
// set Cell properties here
} else {
// Fetch data from feed array
}
}
在您的问题中,您正试图根据AFNetworkReachabilityStatusNotReachable
检测Feed是否已加载。这实际上是不正确的,因为此状态表示没有互联网连接。要跟踪数据的可用性,可以使用上面显示的简单BOOL。