2

我是 iPhone 应用程序开发的新手。我需要开发一个UITableview带有 UISearchbar. 我必须json在 a 中显示一个数组tableView,默认为 aimage和 twobuttons和 two 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;

}
4

2 回答 2

2

您可以将来自 json 的数据放在 dataSource1 数组中,并像下面的代码一样实现搜索栏。和谷歌:- Json Parsing,阅读 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.
    }
于 2013-06-25T06:08:55.523 回答
1

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

AF网络

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

自定义单元格

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

借助以下工具实现搜索栏:

搜索栏

于 2013-06-25T06:02:37.257 回答