UItableview在xib中添加带自定义单元格的搜索栏

时间:2013-06-25 05:24:12

标签: iphone ios uitableview uisearchbar

我是Iphone App Development的新手。我要求使用UITableview开发UISearchbar。我必须在json中显示tableView数组,其默认值为image,两个buttons和两个labels。两个按钮功能是+和 - 一个标签用于显示按钮操作,一个标签用于显示json阵列名称。

确切地说,我需要显示...按钮点击特定单元格的标签计数,并在搜索栏中搜索相同项目时显示相同的计数。

任何人都可以帮助我。请....

提前致谢。

这是我试过的代码..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"ListOfProductsCell";

    ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil) {
            NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        productItemDit=[productListArry objectAtIndex:indexPath.row];
        NSString *offerStr= [NSString stringWithFormat:@"%.2f",[[productItemDit objectForKey:@"offer"] floatValue]];
        NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:@" "] stringByAppendingString:offerStr];
        NSLog(@"%@",fullCostStr);
        cell.itemCostLbl.text=fullCostStr;

        cell.itemStepper.tag=166;
        cell.itemAddedLbl.tag=122;

        itemCount=stepperValueArry.count;
        cell.itemAddedLbl =(UILabel*)[cell viewWithTag:122];
        cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];

    }

    cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];

    if (tableView == self.searchDisplayProduct.searchResultsTableView) {
        searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
        NSLog(@"searchdit:%@",searchProductItemDit);
        cell.itemNameLbl.text= [searchProductItemDit objectForKey:@"name"];
        self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
    } else {
        productItemDit=[productListArry objectAtIndex:indexPath.row];
        NSLog(@"dit:%@",productItemDit);
        cell.itemNameLbl.text=[productItemDit objectForKey:@"name"];

    }

    return cell;

}

2 个答案:

答案 0 :(得分:2)

您可以将来自json的数据放在dataSource1数组中,并像下面的代码一样实现搜索栏。 和谷歌: - Json解析,阅读UITableView和SearchBar的文件,以自定义下面的代码

在ViewController.h中

@interface ViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>{

UITableView *myTableView;
NSMutableArray *dataSource1; //will be storing all the data
NSMutableArray *tableData;//will be storing data that will be displayed in table
NSMutableArray *searchedData;//will be storing data matching with the search string
UISearchBar *sBar;//search bar
}
@property(nonatomic,retain)NSMutableArray *dataSource1;

在ViewController.m

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];
        sBar.delegate = self;
        [self.view addSubview:sBar];

        myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 41, 320, 400)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        [self.view addSubview:myTableView];

        //initialize the two arrays; dataSource will be initialized and populated by appDelegate
        searchedData = [[NSMutableArray alloc]init];
        tableData = [[NSMutableArray alloc]init];
        dataSource1 = [[NSMutableArray alloc]init];
        [dataSource1 addObject:@"vivk"];
        [dataSource1 addObject:@"Balveer"];
        [dataSource1 addObject:@"Balveer"];
        [dataSource1 addObject:@"dharmender"];
        [dataSource1 addObject:@"aaaaa"];
        [dataSource1 addObject:@"aabb"];
        [dataSource1 addObject:@"bbbb"];
        [dataSource1 addObject:@"ba"];
        [dataSource1 addObject:@"ankit"];
        [dataSource1 addObject:@"cccc"];
        [dataSource1 addObject:@"ddddd"];


        [tableData addObjectsFromArray:dataSource1];//on launch it should display all the records
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //NSLog(@"contacts error in num of row");
        return [tableData count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
        return cell;
    }
    #pragma mark UISearchBarDelegate
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        // only show the status bar's cancel button while in edit mode
        sBar.showsCancelButton = YES;
        sBar.autocorrectionType = UITextAutocorrectionTypeNo;
        // flush the previous search content
        [tableData removeAllObjects];
    }
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
        sBar.showsCancelButton = NO;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        [tableData removeAllObjects];// remove all data that belongs to previous search
        if([searchText isEqualToString:@""]||searchText==nil){
            [myTableView reloadData];
            return;
        }
        NSInteger counter = 0;
        for(NSString *name in dataSource1)
        {
            NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(r.location != NSNotFound)
                [tableData addObject:name];
            counter++;
        }
        [myTableView reloadData];
    }
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        // if a valid search was entered but the user wanted to cancel, bring back the main list content
        [tableData removeAllObjects];
        [tableData addObjectsFromArray:dataSource1];
        @try{
            [myTableView reloadData];
        }
        @catch(NSException *e){
        }
        [sBar resignFirstResponder];
        sBar.text = @"";
    }
    // called when Search (in our case "Done") button pressed
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
        [searchBar resignFirstResponder];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

答案 1 :(得分:1)

首先解析您的JSON并将所需的值存储到数组中。尝试使用AFNetworking。

AFNetworking

根据您的要求创建自定义单元格并使用它。

Custom Cell

之后,使用上面的自定义单元格和解析JSON后收到的数组创建一个UITableView

借助以下内容实施搜索栏:

Search Bar