如何提取特定值并填充在tableview中

时间:2012-02-09 12:11:13

标签: iphone objective-c ios xcode ipad

我正在解析包含三个标签的json feed它们是Name,Status,Image并将它存储在NSMutableArray中。如果我的“status”是“1”,我必须返回对应于“Name”标签的数据大括号并填充它我的表视图。问题是如果它的true.im只能返回单个值,它填充整个tableview下面是json结构,代码和截图。

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;

   }

}

enter image description here

1 个答案:

答案 0 :(得分:1)

因为您正在使用for循环,所以您可以立即获得最后一个匹配记录。为什么你在表中使用for循环。您必须检查外部的值并创建一个新数组,然后将该数组分配给表。还发布了一些代码,以便我可以解决您的问题

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

}