【发布时间】:2013-10-15 21:14:17
【问题描述】:
我有一个问题,我创建了一个UITableView 使用CoreData 在视图之间传递数据(row 的UITableView 到DetailView)我使用了这个方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"UpdateData"]) {
NSManagedObject *selectedDevice = [self.datas objectAtIndex:
[[self.tableView indexPathForSelectedRow] row]];
DetailViewController *destViewController = segue.destinationViewController;
destViewController.datas = selectedDevice;
}
}
一切正常。但后来我添加了一个不适用于NSManagedObject 的UISearchBar,所以我创建了另一个NSMutableArray,我在其中保存了所有NSStrings 组成cell.textLabel.text,它可以工作。但是现在我需要修改prepareForSegue,以便每当我从SearchBar tableView 中选择一行时执行segue。
问题是要执行这种segue 与CoreData 的连接,我需要使用NSManagedObject,所以我的问题是:
如何从UITableView 中的UISearchBar 中获取indexPath.row,并使其与self.data 的正确indexPath.row 相对应(这是我用来在@ 中执行正常segue 的数组987654344@,见代码)?
我以为我可以比较单元格的字符串(cell.textLabel.text)但是
- 不知道怎么办。
- 我想如果有 2 行同名,可能会出现问题。
有什么建议吗?
编辑:我从情节提要中添加了 UISearchBar,这是我用来过滤主表视图的代码:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[_searchDati removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
_searchDati = [NSMutableArray arrayWithArray:[_tableData filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
_searchDati 是我为 UISearchBar 创建的数组,_tableData 是我创建的数组,用于存储包含 CoreDatas 的 NSManagedObject 中的单元格名称。
我也添加了这个
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
//Handle selection on searchBar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"UpdateData" sender: self];
}
}
第一个是因为我用了这个,我需要注册类或者给了我一个错误
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
第二个处理 UISearchBarTableView 的选择。
在这里我像之前所说的那样加载 tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
// see filter method
cell.textLabel.text = [_searchDati objectAtIndex:indexPath.row];
} else {
NSString *string;
NSManagedObject *data = [self.datas objectAtIndex:indexPath.row];
string = [NSString stringWithFormat:@"%@", [data valueForKey:@"name"]];
[cell.detailTextLabel setText:nil];
cell.textLabel.text = string;
//store the string to the tableView array
[_tableData addObject:string];
}
return cell;
}
编辑#2:
我写了这段代码
NSIndexPath *indexPath = nil; NSIndexPath *indexPath2 = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
for(int i = 0 ; i < _tableData.count ; i++){
if([self.searchDati[indexPath.row] isEqualToString:_tableData[i]]){
indexPath2 = [NSIndexPath indexPathForRow:i inSection:1];
}
}
NSManagedObject *selectedDevice = [self.datas objectAtIndex:indexPath2.row];
destViewController.datas = selectedDevice;
当我单击 UISearchBar 上的一行时,它仅第一次起作用!我进行搜索并单击该行,它会转到右侧的 DetailView,但如果我再次这样做,它会崩溃并给我这个错误
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
我认为问题在于不是从 indexPath.row == 0 开始,而是在第一次之后创建了一堆。 有什么建议吗?在此先感谢:)
【问题讨论】:
-
如果我正确理解了这个问题,这个问题可能会有所帮助(克里斯的回答对我有用):stackoverflow.com/questions/4471289/…
-
您的搜索栏是如何设置的?您使用的是搜索显示控制器吗?它是否过滤了您的主 tableview?
-
我已经编辑了过滤代码。
标签: ios objective-c uitableview core-data uisearchbar