2013-04-08 39 views
0

我有这个UITableView,我已经从webservice解析数据并加载到NSDictionary中,它的所有工作都很好,数据显示id和imageurl到tableview中问题是logo-id,它是一个int完美展现,当我向下滚动tableview中,但是当我向上滚动的tableview它消失了,当我csroll下再次出现,并在那里停留在UITableView中显示解析的JSON

- (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; 
} 
+0

问题可以这样使用addSubview的。使用'cell.textLabel.text = [dic objectForKey:@“logo-id”]检查;' – 2013-04-08 08:28:12

+0

只是重用单元格而仅删除注释行if(cell == nil){创建并分配单元格} – krishh 2013-04-08 08:32:20

+0

@MidhunMP你是对的它是addsubview问题,请解释原因..sugan.s如果我不评论这些行它显示重复的值 – 2013-04-08 08:47:34

回答

0

如果电池addSubview不起作用试试这个:

[cell.contentView addSubView:lbl]; 

或尝试默认单元格属性:

[email protected]"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; 

}

+0

不行不行@satheeshwaran默认只是工作,但我想定位文本 – 2013-04-08 08:55:52

+0

@ user1440164看到我的编辑,尝试一下并告诉我。 – satheeshwaran 2013-04-08 09:10:59

+0

其工作现在很好的代码,但stil其缺少一个ID后,一些编号 – 2013-04-08 09:17:22