如何提取特定值并填充到表视图中

发布于 2025-01-03 19:03:11 字数 1902 浏览 1 评论 0原文

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

JSON 结构

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

   }

}

在此处输入图像描述

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.i m 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;

   }

}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-01-10 19:03:11

因为您正在使用 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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文