IOS:[self.tableView reloadData]不会重新加载数据

时间:2014-03-30 11:16:30

标签: ios

我正在通过一些教程学习IOS。在searchBar中输入一些文本并点击搜索后,它发送Http请求并解析JSON数据,然后在tableView中显示,但是当我在searchBar中输入一些文本然后点击搜索时, tableView没有显示任何内容。当我在searchBar中再次输入任何内容时,tableView内容显示。

我的问题是,如何在第一次点击搜索但不再输入任何内容时显示内容?谢谢~~ 这是我的代码:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
searchResults = [NSMutableArray arrayWithCapacity:10];

isLoading =YES;
[self.tableView reloadData];
NSURL *url = [self urlWithSearchText: _searchBar.text];
NSString *jsonString = [self performStoreRequestwithUrl:url];

NSDictionary *dictionary = [self parseJSON: jsonString];
[self parseDictionary : dictionary];
[searchResults sortUsingSelector:@selector(compareName:)];
isLoading = NO;
[self.tableView reloadData];

}

- (NSInteger ) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (isLoading) {
    return 1;
}else if (searchResults == nil) {
    return 0;
}else if([searchResults count] == 0 ){
    return 1;
}else{
    return [searchResults count];
}

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

if (isLoading) {
    return [self.tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
}

ResultCell *resultCell =(ResultCell *)[self.tableView
                                             dequeueReusableCellWithIdentifier: @"ResultCell"];
UIImage *image = [UIImage imageNamed:@"TableCellGradient.png"];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage: image];
UIImageView *selectedBackView = [[UIImageView alloc]initWithImage:
                                 [UIImage imageNamed:@"SelectedTableCellGradient.png"]];

resultCell.backgroundView = backGroundView;
resultCell.selectedBackgroundView = selectedBackView;


if ([searchResults count]== 0) {
    resultCell.nameLabel.text = @"Nothing Found";
    resultCell.artistLabel.text = @"";
}else{

    SearchResult * result = [searchResults objectAtIndex: indexPath.row];
    resultCell.nameLabel.text = result.name;
    NSString *kind = [self kindForDisplay:result.kind];
    resultCell.artistLabel.text = [NSString stringWithFormat:@"%@ (%@)",result.artistName,
                                   kind];
}

return resultCell;

}

0 个答案:

没有答案