0

我正在使用 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);
}

提前感谢您的帮助。

干杯

4

1 回答 1

1

考虑根据您的调用是否已完成加载数据来使“无可用数据”字符串动态化。如果呼叫尚未完成,请使用“加载”或类似的东西。如果呼叫已完成,则显示现有的“无可用数据”字符串。

编辑示例:

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

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

将数组设置为 nil 在您的viewDidLoad

- (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表上的方法以显示新数据。

于 2014-05-20T00:34:43.020 回答