2014-01-25 141 views
0

我做了一个表,取决于你点击你的哪个单元格将被发送到一个新的场景(detailviewcontroller)。例如,如果您单击具有文本泰国的单元格,您将被发送到ThailandDetailViewController(场景)。一切工作,直到你使用搜索栏(查看 - (无效)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; 
} 

非常感谢你预先!

回答

0

好了,你可以有一个自定义类存储区和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 

然后您将存储您的公亩totalStrings阵列像这样:

[[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]; 

希望这有助于。

+0

非常感谢,只需要简单的一个问题就可以说下“现在你可以计算出什么类似的激活,如下所示:” – user3161418

+0

不,这只是不好的格式:)现在看编辑版本,也许会让更多感觉这种方式:) – Janos

0

您可以通过将自定义对象存储在表的数据模型而非NSString中来轻松解决此问题。该对象将包含要显示的标签以及一旦选定后激活的赛格名称。

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

+0

你能举个例子吗? – user3161418

+0

好的,我会给出另一个答案,因为在评论中体现代码并不容易。 – Janos