使用Detail View将搜索添加到NSMutableArray

时间:2012-12-01 17:21:46

标签: uitableview ios6 uisearchbar

我有正在处理的项目,我需要将搜索添加到uitableview。我一直在看很多代码,但大多数他们不是我想要的,我无法修改它们。有人可以帮我添加搜索到我的tableview。我也希望从detailview推出searcharray。提前谢谢你。

这里是.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 storyboard,如果这会对你的答案产生影响。

阿德里安

1 个答案:

答案 0 :(得分:1)

对于搜索,创建searchArray和searchText,您还需要一个调用didChange方法的文本字段。

使用此链接将textfield链接到didChange方法:

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

然后在didChange中更新searchText并调用updateSearchArray。

-(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];
}

在tableview方法中使用searchArray,现在使用dummyArray。

更新了tableview方法

#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