UITable 过滤

来源:互联网 发布:python 海森堡模型 编辑:程序博客网 时间:2024/05/22 10:56
#import "RootViewController.h"#import "MyCell.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.    _listArray = [[NSMutableArray arrayWithArray:[UIFont familyNames]] retain];    _data = [_listArray retain];    UITextField *_textFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 2.5, 250, 35)];    _textFiled.borderStyle = UITextBorderStyleRoundedRect;    _textFiled.returnKeyType = UIReturnKeyDone;    _textFiled.clearsOnBeginEditing = YES;    _textFiled.delegate = self;    [_textFiled addTarget:self action:@selector(filter:) forControlEvents:UIControlEventEditingChanged];    self.navigationItem.titleView = _textFiled;    [_textFiled release];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)dealloc{    [super dealloc];}- (void)loadView{    [super loadView];        tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460 - 44) style:UITableViewStylePlain];    tableview.dataSource = self;    tableview.delegate = self;    tableview.separatorColor = [UIColor purpleColor];    [self.view addSubview:tableview];    [tableview release];    UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"edit" style:UIBarButtonItemStyleDone target:self action:@selector(editCell:)];    self.navigationItem.rightBarButtonItem = edit;    [edit release];}- (void)editCell:(id)sender{//    if ([tableview isEditing]){//        [tableview setEditing:NO animated:YES];//    }else{//        [tableview setEditing:YES animated:YES];//    }}#pragma mark UItableView DataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [_listArray count];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellIdentifier = @"cell";    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if (cell == nil){        cell = [[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];        [cell.contentView addSubview:label];        label.tag = 101;        [label release];    }    UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];    label.text = _listArray[indexPath.row];    cell.backgroundColor = [UIColor redColor];    return cell;}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == 2){        return UITableViewCellEditingStyleDelete;    }else{        return UITableViewCellEditingStyleInsert;    }}- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    }- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    id obj = [[_listArray objectAtIndex:sourceIndexPath.row] retain];    [_listArray removeObjectAtIndex:sourceIndexPath.row];    [_listArray insertObject:obj atIndex:destinationIndexPath.row];}#pragma mark UITextField delegate- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}- (void)filter:(UITextField *)textfiled{    [_listArray release];    if ([textfiled.text length] == 0){        _listArray = [_data retain];    }else{        NSString *key = [NSString stringWithFormat:@"self like[c] '%@*'", textfiled.text];        NSPredicate *predicate = [NSPredicate predicateWithFormat:key];        _listArray = [[NSMutableArray arrayWithArray: [_data filteredArrayUsingPredicate:predicate]]retain];    }    [tableview reloadData];}@end

原创粉丝点击