【发布时间】:2012-12-18 12:53:06
【问题描述】:
我有一个实现 UISearchBarDelegate 和 UISearchDisplayDelegate 的表视图控制器。我有一个按钮来切换我的表格视图的编辑视图。在编辑模式下,用户可以选择许多行并复制、删除它们等。如果我尝试在搜索栏中输入内容,而表格视图处于编辑模式,shouldReloadTableForSearchString 方法将触发,然后我重新-使用更新的谓词从核心数据中获取我的数据,并从函数返回 YES,指示应该重新加载表视图。显示过滤结果时,它们不处于编辑模式,即使在 cellForRowAtIndexPath 我可以看到 tableView isEditing 是 YES。我看不到多项选择的圆圈。在选择几个项目之前,我需要能够让用户过滤他们的列表以显着限制结果。
为什么在编辑模式下没有显示结果?
编辑模式,不使用搜索栏:
在编辑模式下搜索会产生不在编辑模式下且未设置样式的结果:
提前致谢, 亚历克斯
这里是 shouldReloadTableForSearchString 方法:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSPredicate *predicate = nil;
if ([searchString length])
{
NSArray* filteredTags = nil;
if ([searchString length] < 3)
{
// Check for tags starting with 1 or 2 characters.
filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
{
if ([[[te.name lowercaseString] substringToIndex:[searchString length]] isEqualToString:[searchString lowercaseString]])
return YES;
return NO;
}]];
}
else
{
// Check for tags with 3 or more consecutive characters anywhere in name
filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
{
if ([[te.name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound)
return YES;
return NO;
}]];
}
// OR ANY(self.tags) in %@
predicate = [NSPredicate predicateWithFormat:
@"ANY(self.songlists) in %@ AND (name contains[cd] %@ OR artist_name contains[cd] %@ OR number = %@ OR ANY(tag_entities) in %@)",
currentSonglist.songs, searchString, searchString, searchString,filteredTags];
[self.fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Return YES to cause the search result table view to be reloaded.
}
return YES;
}
这是 .h 文件中我的核心数据表视图的标题:
@interface SonglistTVC : CoreDataTableViewController
<UISearchBarDelegate,
UISearchDisplayDelegate,
UIPickerViewDataSource,
UIPickerViewDelegate,
UIActionSheetDelegate,
AddSongTVCDelegate,
ListSelectorDelegate>
我知道它正在使用我的表格视图控制器,因为 cellForRowAtIndexPath 和 didSelectRowAtIndexPath 都在触发。我可以在里面进行调试,并且在表格视图中将 isEditing 设置为 YES。但是表格视图并没有反映它正确的编辑模式样式。
这是 cellForRowAtIndexPath 在搜索框中输入字母后立即触发。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"SongCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
Song* song = [self.fetchedResultsController objectAtIndexPath:indexPath];
UILabel* artistLabel = (UILabel*)[cell viewWithTag:ARTIST_NAME_TAG];
UILabel* songLabel = (UILabel*)[cell viewWithTag:SONG_NAME_TAG];
UILabel* keyLabel = (UILabel*)[cell viewWithTag:KEY_TAG];
UILabel* numberLabel = (UILabel*)[cell viewWithTag:NUMBER_TAG];
//artistLabel.font = [UIFont systemFontOfSize:17];
[artistLabel setText: song.artist_name];
[songLabel setText: song.name];
//[songLabel setFont:[UIFont systemFontOfSize:16]];
[keyLabel setText: song.key];
[numberLabel setText: [NSString stringWithFormat:@"%@", song.number]];
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];
UIView *selectedBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];
CAGradientLayer *backGradient = [[CAGradientLayer alloc] init];
backGradient.frame = cell.layer.bounds;
CAGradientLayer *selectedGradient = [[CAGradientLayer alloc] init];
selectedGradient.frame = cell.layer.bounds;
if (song.status == [NSNumber numberWithUnsignedInt: 1])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:240.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 2])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 3])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:200.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
[selectedGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255.0f/255.0f green:160.0f/255.0f blue:60.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.7f] CGColor],
nil]];
if (![self.tableView isEditing] )
{
cell.backgroundView = backView;
[cell.backgroundView.layer insertSublayer:backGradient atIndex:1];
cell.selectedBackgroundView = selectedBackView;
[cell.selectedBackgroundView.layer insertSublayer:selectedGradient atIndex:1];
}
else
{
//cell.backgroundView = backView;
//[cell.backgroundView.layer insertSublayer:backGradient atIndex:1];
cell.selectedBackgroundView = backView;
[cell.selectedBackgroundView.layer insertSublayer:backGradient atIndex:1];
}
for (NSIndexPath* ip in selectedIndexes)
{
[self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
}
[sortOrderPicker reloadAllComponents];
[self sendPickerDownAnimated:NO];
return cell;
}
我使用分段控件内的按钮来打开/关闭编辑:
- (IBAction)segmentChanged:(id)sender{
UISegmentedControl* sg = (UISegmentedControl*)sender;
if (sg.selectedSegmentIndex == 0)
{
// Edit
if (![self.tableView isEditing])
{
[self.tableView setEditing:YES animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
}
else
{
[self.tableView setEditing:NO animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
selectedIndexes = nil;
}
[self.tableView reloadData];
}
else
{
// Sort
if (pickerShown)
[self sendPickerDownAnimated:YES];
else
[self bringPickerUpAnimated:YES];
}
}
【问题讨论】:
-
UISearchDisplayController 为您创建的用于显示搜索结果的表格视图是不同的表格视图。您没有得到样式的事实非常有说服力。听起来您不是在与您认为正在与之交谈的表视图交谈。如果您显示一些代码可能会有所帮助,因为没有人能猜出您是如何做到这一点的...可能您会更乐意创建自己的界面而不使用 UISearchDisplayController...
-
谢谢。我包含了一些代码。我确定正在使用我的表格视图,因为当我单击搜索结果中的行时,didSelectRowAtIndexPath 会正常触发。在那个时候,isEditing 仍然是 YES。
-
你能分享你使表格可编辑和不可编辑的代码吗?
-
是的。我把它加到最后。这几乎就是:[self.tableView setEditing:YES animated:YES];
-
对,但是我刚才说了UISearchDisplayController为你创建的table view是一个不同的table view。设置有关
self.tableView的内容不会对其产生任何影响。这就是我的重点......didSelectRow是一个委托方法,所以这无关紧要。我没有说你不是这个表格视图的代表;你是,自动的。我说这是一个不同的表格视图,您可能需要意识到这一事实的含义。
标签: objective-c ios xcode uitableview uisearchbar