0

我有我正在处理的项目,我需要将搜索添加到我的uitableview. 我一直在查看很多代码,但大多数都不是我想要的,我无法修改它们。有人可以帮我将搜索添加到我的tableview. 我也想推detailviewfrom searcharray。先感谢您。

这是.huitableview

#import <UIKit/UIKit.h>

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

@end

这是.muitableview

#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

这是.hdetailview

#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

最后是.mdetailview

#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 故事板。

阿德里安

4

1 回答 1

1

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

使用它将文本字段链接到 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];
}

在您现在使用 dummyArray 的 tableview 方法中使用 searchArray。

更新了 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



于 2012-12-05T10:23:34.280 回答