UITableView在拥有数据之前不会弹出任何数据

时间:2014-05-20 00:28:26

标签: ios objective-c uitableview hpple

我正在使用hpple从网页获取数据并将其显示在表视图中,但是如果没有数据或者无法获取数据,我希望单元格显示“无数据”。但正因为如此,当数据加载到这里时,它会使视图控制器上没有数据大约一秒钟是我正在使用的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            if ((_deli.count > 0)) {
                return _deli.count;
            }
            else {
                return 1;
            }
            break;
        case 1:
            if ((_entrees.count > 0)) {
                return _entrees.count;
            }
            else {
                return 1;
            }
            break;
     }
     return 0
}

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    //[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:17.0f];

    if (indexPath.section == 0) {
        if ([tableView numberOfRowsInSection:0] == _deli.count ) {
            Items *thisDeli = [_deli objectAtIndex:indexPath.row];
            cell.textLabel.text = thisDeli.title;

        }
        else { cell.textLabel.text = @"No Data Avaiable";
        }

    }
    else if (indexPath.section == 1) {
        if ([tableView numberOfRowsInSection:1] == _entrees.count) {
            Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
            cell.textLabel.text = thisEntree.title;


        }
        else { cell.textLabel.text = @"No Data Avaiable";
        }

    }
}

是否有办法延迟“无数据”短语的出现,或者只有在空白或无法获取时才显示。

继承我的NSURLConnection

- (void) loadDataUsingNSURLConnection {
    NSString *url = @"http://ueat.site88.net/westTest.xml";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"response == %@", response);
}

提前感谢您的帮助。

干杯

1 个答案:

答案 0 :(得分:1)

考虑制作"无可用数据"字符串动态,取决于您的调用是否已完成加载数据。如果呼叫尚未完成,则使用"正在加载"或类似的东西。如果通话已完成,则表明已存在"无可用数据"串。

编辑示例:

使用您拥有的代码,最简单的方法是将_deli_entrees变量设置为nil,直到您加载它们为止,并检查显示消息时的条件:

@interface YourViewController ()
{
    NSArray *_deli;
    NSArray *_entrees;
}
@end

viewDidLoad

中将数组设置为nil
- (void)viewDidLoad
{
    [super viewDidLoad];
    _deli = nil;
    _entrees = nil;
}

将之前代码中的逻辑更改为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            if (_deli == nil || _deli.count == 0) {
                return 1;
            }
            else {
                return _deli.count;
            }
            break;
        case 1:
            if (_entrees == nil || _entrees.count == 0) {
                return 1;
            }
            else {
                return _entrees.count;
            }
            break;
     }

     return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    //[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:17.0f];

    if (indexPath.section == 0) {
        if (_deli == nil)
            cell.textLabel.text = @"Loading";
        else if (_deli.count > 0) {
            Items *thisDeli = [_deli objectAtIndex:indexPath.row];
            cell.textLabel.text = thisDeli.title;
        }
        else 
            cell.textLabel.text = @"No Data Avaiable";
    }
    else if (indexPath.section == 1) {
        if (_entrees == nil)
            cell.textLabel.text = @"Loading";
        else if (_entrees.count > 0) {
            Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
            cell.textLabel.text = thisEntree.title;
        }
        else
            cell.textLabel.text = @"No Data Avaiable";
    }
}

由于您的loadDataUsingNSURLConnection只是写出了回复,我不确定您是如何加载数组的,但此时您应该明白这一点。您只需将响应加载到数组中并调用表上的reloadData方法即可显示新数据。