2013-04-08 68 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