【发布时间】:2012-02-02 17:28:17
【问题描述】:
我正在做一个学生项目。
此时我们的应用程序可以调用 API 并重新加载数据,但前提是我调用 textDidChange 中的 XMLParser。
含义:每次在 UISearchBar 中键入一个字母时,它都会正确调用和重新加载。我希望调用和重新加载仅在用户单击搜索按钮时发生,但在 textDidChange 中工作的相同代码在 searchBarSearchButtonClicked 中不起作用。
相反.. 该方法仅在按下搜索按钮 (good) 时调用 API,接收与 textDidChange 相同的信息 (good) 但不会重新加载 UITableView (坏)。
我已经到处搜索了我的问题的答案,所以我想我会发布一个问题:)
我遇到的所有示例仅显示了当用户键入(联系人列表)时如何显示与用户条件匹配的数组的内容,但没有显示如何使用搜索按钮正确重新加载 uitableView。
为什么相同的代码在委托方法中正确重新加载
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
但不在
- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar?
正如我所说,当单击 searchButton 时,NSLog 会打印出正确的数据以加载 uitableview,所以这不是问题。
任何人都可以在这里指出正确的方向吗? :)
我的 cellForRowAtIndexPath:
`
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifier = @"searchCell";
SearchCell *cell = nil;
cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if(!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SearchCell class]]) {
cell = (SearchCell *)currentObject;
break;
}
}
}
Search *currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];
cell.track_label.text = [currentSearch track];
cell.artist_label.text = [currentSearch artist];
return cell;
}
请求的委托方法:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
NSString *searchurl = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track?q=%@", [self urlEncodeValue:searchText]];
xmlParser = [[SearchXMLParser alloc] loadXMLByURL:searchurl];
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
NSString *searchurl = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track?q=%@", [self urlEncodeValue:theSearchBar.text]];
xmlParser = [[SearchXMLParser alloc] loadXMLByURL:searchurl];
[self.tableView reloadData];
[theSearchBar setShowsCancelButton:NO animated:YES];
[theSearchBar resignFirstResponder];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[searchxmlParser searchhits] count];
}
感谢您的宝贵时间! :)
【问题讨论】:
-
是的,感谢您在我编辑您的问题时花费的时间
-
您使用搜索显示控制器吗?还是不行?
-
谢谢 :) 是的,我正在使用搜索显示控制器 :)
-
索引路径处的行单元格看起来如何?您是否测试是否显示女巫表视图?添加一些代码..
-
@terenteIonutAlexandru 兄弟,你没有得到所有这些。 @_AlexanderNorway 我建议在这两种委托方法中发布您的代码,以便我们可以看到您在那里做错了什么。
标签: iphone api uitableview uisearchbar