在UITableView中显示已解析的JSON

时间:2013-04-08 08:15:09

标签: ios json uitableview

我有这个UITableView我在其中解析了来自web服务的数据并加载到NSDictionary中它的工作正常数据显示id和imageurl进入tableview问题是logo-id这是一个int显示当我滚动时完美在桌面视图中,但是当我向上滚动桌面视图时,它会消失,当我向下滚动时,它会再次出现并停留在那里

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //    }
    // Configure the cell... setting the text of our cell's label

    NSDictionary *dic = [json objectAtIndex:[indexPath row]];


    NSString *strImage = [dic objectForKey:@"image-name-small"];
    NSURL *ImageURL = [NSURL URLWithString:[strImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    AsyncImageView *asyncImgView = [[AsyncImageView alloc] initWithFrame:CGRectMake(3, 10, 100, 100)];
    asyncImgView.contentMode = UIViewContentModeScaleAspectFit;
    asyncImgView.backgroundColor =  [UIColor clearColor];
    [asyncImgView loadImageFromURL:ImageURL];





    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 200, 100, 25)];
    lbl.text = [dic objectForKey:@"logo-id"];
    [cell addSubview:lbl];
    [cell addSubview:asyncImgView];
    cell.textLabel.text = @"";
    return cell;
}

1 个答案:

答案 0 :(得分:0)

如果单元格addSubview不起作用,请尝试:

[cell.contentView addSubView:lbl];

或尝试默认的单元格属性:

cell.textLabel.text=@"sample text";

//试试这段代码:

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}
else
{
    for (UIView *subview in cell.contentView.subviews)
        [subview removeFromSuperview];
}

NSDictionary *dic = [json objectAtIndex:[indexPath row]];


NSString *strImage = [dic objectForKey:@"image-name-small"];
NSURL *ImageURL = [NSURL URLWithString:[strImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
AsyncImageView *asyncImgView = [[AsyncImageView alloc] initWithFrame:CGRectMake(3, 10, 100, 100)];
asyncImgView.contentMode = UIViewContentModeScaleAspectFit;
asyncImgView.backgroundColor =  [UIColor clearColor];
[asyncImgView loadImageFromURL:ImageURL];

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 200, 100, 25)];
lbl.backgroundColor=[UIColor redColor];
lbl.text = [dic objectForKey:@"logo-id"];

[cell.contentView addSubview:lbl];
[cell.contentView addSubview:asyncImgView];

return cell;

}