2014-01-25 134 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