简体   繁体   中英

How to extract specific values and populate in tableview

I m parsing a json feed containing three tags they are Name,Status,Image and storing it in an NSMutableArray.if my "status" is "1" ,i got to return the data corresponding to the "Name" tag within the braces and populate it my table view.the problem is if its true.im able to return only single value and it populates the entire tableview below is the json structure,code and screenshot.

JSON STRUCTURE

{
    Image = "http://dev-parkguiden.knutpunkten.se/Content/Images/Icons/icon8.png";
    Name = "Caf\U00e9/restaurang";
    Status = 0;
},
    {
    Image = "http://dev-parkguiden.knutpunkten.se/Content/Images/Icons/icon9.png";
    Name = "Kiosk ";
    Status = 0;
},
    {
    Image = "http://dev-parkguiden.knutpunkten.se/Content/Images/Icons/icon10.png";
    Name = "Toalett finns";
    Status = 1;
},
    {
    Image = "http://dev-parkguiden.knutpunkten.se/Content/Images/Icons/icon11.png";
    Name = "Parkeringsm\U00f6jligheter";
    Status = 1;
},
    {
    Image = "http://dev-parkguiden.knutpunkten.se/Content/Images/Icons/icon1.png";
    Name = Minigolf;
    Status = 1;
},

XCODE

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

}

for ( int i=0; i<[self.media1 count]; i++) 
{

    NSDictionary *boy=[self.media1 objectAtIndex:i];


NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
    //NSLog(@"the value:%@",str);

    if([str isEqualToString:@"1"])
    { 
        //NSDictionary *hai=[[boy objectAtIndex:indexPath.row]objectForKey:@"1"];
    //  NSLog(@"the values availbale:%@",hai);
        cell.textLabel.text=[self.story objectAtIndex:i];
            return cell;

   }

}

在此处输入图片说明

because of you are using for loop you get the last match records at once. why you are using for loop in table. you have to check the values outside and make a new array and then assign that array in to table. also post some more code so i can solve your problem

-(void)makeNewArray
{
for ( int i=0; i<[self.media1 count]; i++) 
{

    NSDictionary *boy=[self.media1 objectAtIndex:i];


    NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
    if([str isEqualToString:@"1"])
    { 
        [newArray addObject:[self.story objectAtIndex:i]];
    }
}
[tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return newArray.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] autorelease];

}
        cell.textLabel.text=[newArray objectAtIndex:indexPath.row];
            return cell;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM