Objective c Segue不';因为搜索栏,所以无法工作

Objective c Segue不';因为搜索栏,所以无法工作,objective-c,object,search,segue,Objective C,Object,Search,Segue,我已经制作了一个表格,根据您单击的单元格,您将被发送到一个新场景(detailviewcontroller)。例如,如果单击包含文本Thailand的单元格,则会将您发送到ThailandDetailViewController(场景)。在使用搜索栏(在-(void)tableView下查看)之前,一切都正常 -当一些国家被过滤掉时(因为搜索功能),扩大的国家将在表格中上升,并获得较低的计数。这导致它们将导致错误的detailviewcontroller(场景) 我的一个朋友对我说,我应该在数组

我已经制作了一个表格,根据您单击的单元格,您将被发送到一个新场景(detailviewcontroller)。例如,如果单击包含文本Thailand的单元格,则会将您发送到ThailandDetailViewController(场景)。在使用搜索栏(在-(void)tableView下查看)之前,一切都正常

-当一些国家被过滤掉时(因为搜索功能),扩大的国家将在表格中上升,并获得较低的计数。这导致它们将导致错误的detailviewcontroller(场景)

我的一个朋友对我说,我应该在数组中使用objectAtIndex,但他没有真正理解这是什么意思。。然后打开cell.textLabel.text(我没有真正跟着他)

这是我的.m文件:

- (void)viewDidLoad
{
[super viewDidLoad];
self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;

totalStrings = [[NSMutableArray alloc] initWithObjects:@"America",@"Austria",@"Canada",@"France",@"Germany",@"Greece",@"Malaysia",@"Mexico",@"Netherlands",@"Poland",@"Russia",@"Singapore",@"Thailand",@"Ukraine", nil];    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

switch (indexPath.row) {
    case 0: [self performSegueWithIdentifier:@"Segue0" sender:self];
        break;
    case 1: [self performSegueWithIdentifier:@"Segue1" sender:self];
        break;
    //and so on
    default: break;
}
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0){
    isFiltered = NO;
}
else
{
    isFiltered = YES;
    filteredStrings = [[NSMutableArray alloc]init];
    for (NSString *str in totalStrings){
        NSRange stringRange = [str rangeOfString:searchText      options:NSCaseInsensitiveSearch];
        if(stringRange.location !=NSNotFound) {
             [filteredStrings addObject:str];
        }
    }
}
[self.myTableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
static NSString *Cellidentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}
if (!isFiltered) {
    cell.textLabel.text = [totalStrings objectAtIndex:indexPath.row];
}
else //if it's filtered
{
    cell.textLabel.text = [filteredStrings objectAtIndex:indexPath.row];
}
return cell;
}

事先非常感谢你

通过在表的数据模型中存储自定义对象而不是NSString,可以轻松解决此问题。该对象将包含要显示的标签以及选定后要激活的序列的名称


这是另一个问题,为什么不同的数据需要完全不同的视图控制器。我想这些是不同类型的数据,需要不同的方法来处理它们。

好吧,您可以有一个自定义类来存储区域和segue索引,如下所示:

@interface SegueVO : NSObject
@property (nonatomic, strong) NSString *area;
@property int segueIndex;
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index;
@end

@implementation SegueVO
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index
{
    self = [super init];
    if (self)
    {
        self.area = area;
        self.segueIndex = index;
    }
    return self;
}
@end
[[NSMutableArray alloc] initWithObjects:[[SegueVO alloc] initWithArea:@"America" andIndex:0],....
NSArray *arrayToUse = totalStrings;
if (isFiltered)
   arrayToUse = filteredStrings;

[self performSegueWithIdentifier:[@"Segue" 
 stringByAppendingString:[NSString stringWithFormat:@"%i", 
 [arrayToUse[indexPath.row].segueIndex]] sender:self];
然后将战神存储在totalStrings数组中,如下所示:

@interface SegueVO : NSObject
@property (nonatomic, strong) NSString *area;
@property int segueIndex;
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index;
@end

@implementation SegueVO
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index
{
    self = [super init];
    if (self)
    {
        self.area = area;
        self.segueIndex = index;
    }
    return self;
}
@end
[[NSMutableArray alloc] initWithObjects:[[SegueVO alloc] initWithArea:@"America" andIndex:0],....
NSArray *arrayToUse = totalStrings;
if (isFiltered)
   arrayToUse = filteredStrings;

[self performSegueWithIdentifier:[@"Segue" 
 stringByAppendingString:[NSString stringWithFormat:@"%i", 
 [arrayToUse[indexPath.row].segueIndex]] sender:self];
当然,您可以创建一个工厂方法来减少初始化代码

现在,您可以像这样确定要激活的序列:

@interface SegueVO : NSObject
@property (nonatomic, strong) NSString *area;
@property int segueIndex;
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index;
@end

@implementation SegueVO
-(id)initWithArea:(NSString *)area andSegueIndex:(int)index
{
    self = [super init];
    if (self)
    {
        self.area = area;
        self.segueIndex = index;
    }
    return self;
}
@end
[[NSMutableArray alloc] initWithObjects:[[SegueVO alloc] initWithArea:@"America" andIndex:0],....
NSArray *arrayToUse = totalStrings;
if (isFiltered)
   arrayToUse = filteredStrings;

[self performSegueWithIdentifier:[@"Segue" 
 stringByAppendingString:[NSString stringWithFormat:@"%i", 
 [arrayToUse[indexPath.row].segueIndex]] sender:self];

希望这能有所帮助。

你能举个例子吗?好的,我会给出另一个答案,因为在注释中正确地放置代码是不容易的。非常感谢,只需要一个简短的问题,它是否应该在“现在你可以确定激活的顺序:”不,只是格式真的不好:)现在查看编辑版本,这样可能更有意义:)