2012-08-28 3 views
1

xml을 구문 분석하고 표 셀에 제목, 설명 및 이미지를 표시하고 싶습니다.두 개의 다른 섹션에있는 두 개의 서로 다른 NSMutableArray의 셀에 내용 (두 개의 레이블과 한 개의 이미지 뷰)을 표시하는 방법.

코드를 사용했지만 이미지를 사용할 수없는 경우 셀에도 쓰레기 이미지가 표시됩니다.

숨기기 위해 코드를 숨기지 않았지만 이미지보기는 숨기지 않았습니다.

또한 나는 제로 값으로 프레임을 설정하지만, 쓰레기 이미지와 이미지보기 표시됩니다 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    appDelegate = (AppDelegate_iPhone *) [[UIApplication sharedApplication] delegate]; 

    if (section == 0) { 
     return [appDelegate.TrendDataArray count]; 
    }else { 
     return [appDelegate.MyDataArray count]; 
    } 

} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

{ 

    if(section == 0) 

     return @" Trending "; 

    else 

     return @" Live Feeds "; 

} 

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

    UILabel *lblTitle; 
    UILabel *lbldescription; 
    UIImageView *imgViewTemp; 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     lblTitle = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [lblTitle setLineBreakMode:UILineBreakModeWordWrap]; 
     [lblTitle setNumberOfLines:2]; 
     lblTitle.textAlignment= UITextAlignmentLeft; 
     [lblTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT]]; 
     lblTitle.textColor=[UIColor blackColor]; 
     [lblTitle setTag:1]; 

     lbldescription = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [lbldescription setLineBreakMode:UILineBreakModeWordWrap]; 
     [lbldescription setNumberOfLines:3]; 
     lbldescription.textAlignment= UITextAlignmentLeft; 
     [lbldescription setFont:[UIFont fontWithName:@"Helvetica" size:FONT2]]; 
     lbldescription.textColor=[UIColor grayColor]; 
     [lbldescription sizeToFit]; 
     [lbldescription setTag:2]; 

     imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectZero]; 
     [imgViewTemp setTag:3]; 


     [cell.contentView addSubview:lblTitle]; 
     [cell.contentView addSubview:lbldescription]; 

     //[imgViewTemp release]; 
     [lblTitle release]; 
     [lbldescription release]; 

    } 

    // Configure the cell... 
    if (indexPath.section == 0) { 

     appDelegate=(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
     objXmlParse_Attributes= [appDelegate.TrendDataArray objectAtIndex:indexPath.row]; 

    }else if (indexPath.section == 1) { 

     appDelegate=(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
     objXmlParse_Attributes= [appDelegate.MyDataArray objectAtIndex:indexPath.row]; 

    } 


    NSString *text = objXmlParse_Attributes.CommenUrlTitle; 
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    NSString *text2 = objXmlParse_Attributes.CommenUrlDescription; 
    text2 = [text2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    CGSize constraint = CGSizeMake(WIDTH - (MARGIN * 2), 20000.0f); 
    CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
    CGSize size2 = [text2 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:FONT2] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    NSString *link = [NSString stringWithFormat:@"%@", objXmlParse_Attributes.CommenUrlImage]; 

    //imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectZero]; 
    //[UIImage imageNamed:@"noimage.png"] 



    if([link isEqualToString:@"No image"] || [link isEqualToString:@"no image"]){ 
     link = @""; 
     if ([link isEqualToString:@""]) { 

      imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,0,0)]; 

     //[imgViewTemp release]; 

     lblTitle = (UILabel*)[cell.contentView viewWithTag:1]; 
     [lblTitle setText:text]; 
     [lblTitle setFrame:CGRectMake(MARGIN+3,MARGIN, WIDTH-110+83,MIN(size.height,30))]; 

     lbldescription = (UILabel*)[cell.contentView viewWithTag:2]; 
     [lbldescription setText:text2]; 
     [lbldescription setFrame:CGRectMake(MARGIN+3,MARGIN + MIN(size.height,30),WIDTH-110+83,MIN(size2.height,40))]; 
    } 
    } 
    else{ 

     //imgViewTemp = (UIImageView*)[cell.contentView viewWithTag:3]; 
     imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectMake(7,11,73,60)]; 
     [imgViewTemp setImageWithURL:[NSURL URLWithString:link] placeholderImage:nil]; 
     //image1.hidden=FALSE; 

     lblTitle = (UILabel*)[cell.contentView viewWithTag:1]; 
     [lblTitle setText:text]; 
     [lblTitle setFrame:CGRectMake(MARGIN + 83, MARGIN, WIDTH-110,MIN(size.height,30))]; 

     lbldescription = (UILabel*)[cell.contentView viewWithTag:2]; 
     [lbldescription setText:text2]; 
     [lbldescription setFrame:CGRectMake(MARGIN + 83, MARGIN + MIN(size.height,30),WIDTH-110,MIN(size2.height,40))]; 

     [cell.contentView addSubview:imgViewTemp]; 

    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

답변

1

이봐 단지 테이블을 만들기 위해 튜토리얼을 검색합니다. 이 부분에서 그러한 쿼리를 묻지 마십시오. 많은 예제가 UITableView ... 있습니다. 이해해 주시기 바랍니다.

+0

내가 용액 을 사용하여 해결되면 (제 == 0) {배열 1 에서 라벨 이미지를 추가하기위한 // 여기 내 코드} 경우 라벨을 추가 (제 == 1) { // 여기 내 코드 array2의 이미지 } –

+0

ok 축하합니다 ... – Rajneesh071

관련 문제