2012-12-01 63 views
0

我有我正在項目,我需要添加搜索到我的uitableview。我一直在看很多代碼,但大多數他們不是我正在尋找,我不能修改它們。有人可以幫我添加搜索到我的tableview。我也想推detailviewsearcharray。先謝謝你。添加搜索NSMutable陣列與DetailView

這裏是.H uitableview

#import <UIKit/UIKit.h> 

@interface Reds : UITableViewController { 
NSMutableArray *dummyArray; 
} 
- (void) setupData; 

@end 

這裏.M uitableview

#import "Reds.h" 
#import "RedsDetail.h" 

@interface Reds() 

@end 

@implementation Reds 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self setupData]; 
} 

- (void) setupData { 
dummyArray = [[NSMutableArray alloc] init]; 

[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 1", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]]; 
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 2", @"name" , @"image1.JPG", @"image" , @"dummy 2 description textview", @"description", nil]]; 
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 3", @"name" , @"image1.JPG", @"image" , @"dummy 3 description textview", @"description", nil]]; 

} 
- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [dummyArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

cell.textLabel.text = [[dummyArray objectAtIndex:indexPath.row] objectForKey:@"name"]; 
return cell; 
} 

#pragma mark - Table view delegate 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if([[segue identifier] isEqualToString:@"DummyDetail"]){ 
    RedsDetail *dummyDetail = [segue destinationViewController]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
    dummyDetail.dummyImageString = [[NSString alloc] initWithString:[[dummyArray objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
    dummyDetail.dummyTextString = [[NSString alloc] initWithString:[[dummyArray objectAtIndex:indexPath.row] objectForKey:@"description"]]; 
    dummyDetail.title = [[NSString alloc] initWithString:[[dummyArray objectAtIndex:indexPath.row] objectForKey:@"name"]]; 
} 
} 

@end 

這裏是.H detailview

#import <UIKit/UIKit.h> 

@interface RedsDetail : UIViewController { 

IBOutlet UIImageView *dummyImage; 
IBOutlet UITextView *dummyText; 
NSString *dummyImageString; 
NSString *dummyTextString; 

} 

@property (nonatomic, retain) NSString *dummyImageString; 
@property (nonatomic, retain) NSString *dummyTextString; 
@end 

終於.M detailview

#import "RedsDetail.h" 

@interface RedsDetail() 

@end 

@implementation RedsDetail 
@synthesize dummyImageString, dummyTextString; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

dummyImage.image = [UIImage imageNamed:dummyImageString]; 
dummyText.text = dummyTextString; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
} 

@end 

我知道這對大多數人來說可能是一個簡單的答案,但我無法弄清楚,我需要幫助。我真的很感謝你們花時間幫助我。我也使用xcode 4.5 ios 6故事板,如果這使你的答案有所不同。

阿德里安

+0

我認爲這篇文章將引領你在正確的方向:HTTP://計算器。com/questions/2609958/how-to-search-through-a-nsmutablearray –

+0

感謝大衛,但我一直在尋找代碼片段的幫助。我已經通過文檔,但我很難做到這一點,而無需改變我的班級結構。我正試圖避免這種情況。但是如果我不能得到任何幫助,我可能必須:( –

+0

其他人都在意幫忙嗎? –

回答

1

對於搜索,創建searchArray和SEARCHTEXT,也需要調用該方法didChange一個文本框。然後

[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

在didChange更新SEARCHTEXT並調用updateSearchArray:

使用此到文本框鏈接到didChange方法。

-(void)textFieldDidChange:(UITextField*)textField 
{ 
    searchTextString = textField.text; 
    [self updateSearchArray]; 
} 

在updateSearchArray你比較經過原數組(dummyArray),如果該名稱包含在搜索中,然後將其添加到searchArray。

-(void)updateSearchArray 
{ 
    if (searchTextString.length != 0) { 
     searchArray = [NSMutableArray array]; 
     for (NSDictionary* item in dummyArray) { 
      if ([[[item objectForKey:@"name"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound) { 
       [searchArray addObject:item]; 
      } 
     } 
    } else { 
     searchArray = dummyArray; 
    } 

    [self.tableView reloadData]; 
} 

使用searchArray的實現代碼如下方法,你現在使用的dummyArray。

更新實現代碼如下方法

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [searchArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.textLabel.text = [[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"]; 
    return cell; 
} 

此外,你需要將所有的數據到數組後調用reloadData。並增加了updateSearchArray方法

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupData]; 
    [self updateSearchArray]; 
    [self.tableView reloadData]; 
} 


爲了更容易實現這個我做了一個樣本項目。你可以在這裏下載:http://www.rolandkeesom.com/SearchExample.zip



+0

感謝您的回覆。您能否將代碼插入到我提供的樣本數據中,並告訴我它是如何完成的,因爲我可以看起來這樣做是正確的,我很沮喪,這應該是非常簡單,我不能這樣做,這就是爲什麼我提供了一個賞金,我即將放棄這組代碼:( –

+0

更新後,讓我知道它是否有效 –

+0

我試過你的更新代碼,我得到了幾個警告和一個錯誤,我用你提供的代碼編輯了我的帖子,你可以請看看。 –