0

我有这个 UITableView,我在其中解析了来自 web 服务的数据并加载到 NSDictionary 中,它一切正常,数据显示 id 和 imageurl 到 tableview 问题是 logo-id,当我向下滚动 tableview 时,它是一个 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;
}
4

1 回答 1

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;

}

于 2013-04-08T08:50:18.637 回答