2013-07-24 2 views
0

서버에서 이미지를로드 할 UITableView가 있습니다. 하지만 때로는 UITableView에 표시 할 이미지가 없으며 UILabel을 표시하려고합니다. 이걸 어떻게 성취 할 수 있을지 궁금해. 이를 달성하기 위해 도움이나 코드 조각을 주시면 감사하겠습니다. 대단히 감사합니다!UIImage가 없을 때 UILabel을 표시하는 방법

나는 당신이 말한 것을 시도했다. 테이블을로드 할 때 처음에는 모든 것이 잘 작동하지만 모든 라벨을 스크롤하면 버튼이 모든 곳으로 이동합니다.
여기 내 코드입니다. 당신의 cellForRowAtIndexPath 방법에서

- (UITableViewCell *)tableView:(UITableView *)tableView       cellForRowAtIndexPath:(NSIndexPath *)indexPath 
       { 
       static NSString *CellIdentifier = @"Cell"; 
       UITableViewCell *cell = [self.tableView           dequeueReusableCellWithIdentifier:CellIdentifier]; 
       if (cell == nil) { 
       cell = [[UITableViewCell alloc]             initWithStyle:UITableViewCellStyleDefault          reuseIdentifier:CellIdentifier]; 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 

       } 
       if (msgImgFile){ 
       NSLog (@"Image file found!"); 
       lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, 200,    20)]; 
       lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 378, 150,    20)]; 
       lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 398,     150, 20)]; 
       btnPlayStop.frame = CGRectMake(255.0f, 375.0f, 30.0f, 30.0f); 
       } 
       else 
       { 
       NSLog(@"Image file not found. Simply load the UILabel and      UIButton"); 
       lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200,     20)]; 
       lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 68, 150,     20)]; 
       lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 88, 150,    20)]; 
       btnPlayStop.frame = CGRectMake(255.0f, 45.0f, 30.0f, 30.0f); 

       } 
       lblOne.font = [UIFont fontWithName:@"Arial" size:12]; 
       [lblOne setBackgroundColor:[UIColor clearColor]]; 
       lblOne.tag = 1; 


       lblTwo.font = [UIFont fontWithName:@"Arial" size:12]; 
       [lblTwo setBackgroundColor:[UIColor clearColor]]; 
       lblTwo.tag = 2; 

       lblThree.font = [UIFont fontWithName:@"Arial" size:10]; 
       [lblThree setBackgroundColor:[UIColor clearColor]]; 
       lblThree.tag = 3; 

       lblFour = [[UILabel alloc] initWithFrame:CGRectMake(10, 24, 150,    20)]; 
       lblFour.font = [UIFont fontWithName:@"Arial" size:12]; 
       [lblFour setBackgroundColor:[UIColor clearColor]]; 
       lblFour.tag = 4; 

       btnPlayStop = [UIButton buttonWithType:UIButtonTypeCustom];   
       [btnPlayStop setTitle:@"Play" forState:UIControlStateNormal]; 
       [btnPlayStop setImage:[UIImage imageNamed:@"Play Button.png"]     forState:UIControlStateNormal]; 
       [btnPlayStop addTarget:self action:@selector(playRecordClicked:)    forControlEvents:UIControlEventTouchUpInside]; 

       [cell addSubview:lblOne]; 
       [cell addSubview:lblTwo]; 
       [cell addSubview:lblThree]; 
       [cell addSubview:lblFour]; 
       [cell.contentView addSubview:btnPlayStop]; 

       dispatch_queue_t queue =              dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); 
       dispatch_async(queue, ^{ 

       msgObjImg = (PFObject *)[self.imageDataMutArray         objectAtIndex:indexPath.row]; 
       createdDt = msgObjImg.createdAt; 


       msgImgFile = [msgObjImg objectForKey:@"siqImage"]; 
       NSData *imgData = [msgImgFile getData]; 

       UIImage *msgImgFound = [UIImage imageWithData:imgData];    
       UIImage *newImg = [self scaleImage:msgImgFound         toSize:CGSizeMake(280.0, 300.0)]; 

       dispatch_sync(dispatch_get_main_queue(), ^{ 

       UILabel *dtTimeLabel = (UILabel *)[cell viewWithTag:3]; 
       NSDateFormatter *dtFormat = [[NSDateFormatter alloc]init]; 
       [dtFormat setDateFormat:@"MM-dd-yyyy HH:mm"]; 
       [dtFormat setTimeZone:[NSTimeZone            timeZoneForSecondsFromGMT:-18000]]; 
       NSString *createdDtString = [dtFormat stringFromDate:createdDt]; 
       dtTimeLabel.text = [NSString stringWithFormat:@"Received on:     %@",createdDtString]; 
       [[cell imageView] setImage:newImg]; 
       [cell setNeedsLayout]; 
       } 
        return cell; 

       } 
+0

당신의'cellForRowAtIndexPath' 방법에있는 당신의 이미지를 얻을 다른 곳을 잡고 –

+0

후있는 tableView를 다시로드 호출을하지 말아야은 시도 것을 줄 것이다 . 시간 내 주셔서 감사합니다. – user2562080

답변

0

이미지가 서버에서로드를 완료
UIImage *image = [imagesArray objectAtIndex:indexPath.row]; 

if (image) { 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourImageViewFrame];//create desired Frame 
    imageView.image = image; 
    [cell addSubview:imageView]; 

} else { 

    UILabel *label = [[UILabel alloc] initWithFrame:yourLabelFrame];//create desired frame 
    label.text = @"No Image"; 
    [cell addSubview:label]; 

} 

, 어디에 문제가 [tableView reloadData];

+0

이 내용을 if (cell == nil)에 포함시켜야합니까? { cell = [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : CellIdentifier]; } 아니면 루프 외부에 보관 하시겠습니까? – user2562080

+0

네, 이미 거기에있는 셀을 만들고 있다고 가정했습니다. –

+0

'cellForRowAtIndexPath' 메소드의 시작 부분에있을 것입니다. –

1

은 내가 볼 수있는 호출하고 당신에 대한 권리 절차를 알 수 있습니다 이것을 해결하십시오.

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

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

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

    lblOne = [[UILabel alloc]init]; 
    lblOne.tag = 1; 

    lblTwo = [[UILabel alloc]init]; 
     lblTwo.tag = 2; 
lblThree = [[UILabel alloc]init]; 
    lblThree.tag = 3; 
    lblFour = [[UILabel alloc]init]; 
    lblFour.tag = 4; 
    btnPlayStop = [[UILabel alloc]init]; 
    btnPlayStop.tag = 5; 

// Perform addition functions (your requirements) 

       [cell.contentView addSubview:lblOne]; 
       [cell.contentView addSubview:lblTwo]; 
       [cell.contentView addSubview:lblThree]; 
       [cell.contentView addSubview:lblFour]; 
       [cell.contentView addSubview:btnPlayStop]; 

    } 
    else 
    { 
     lblOne = (UILabel*)[cell.contentView viewWithTag:1]; 
     lblTwo = (UILabel*)[cell.contentView viewWithTag:2]; 
     lblThree = (UILabel*)[cell.contentView viewWithTag:3]; 
     lblFour = (UILabel*)[cell.contentView viewWithTag:4]; 
     btnPlayStop = (UILabel*)[cell.contentView viewWithTag:5]; 

// AND SO ON ... 
    } 

// SET THE VALUES HERE FOR YOUR CONTENT VALUES 

    return cell; 
} 

동적 내용보기를 사용하여 셀을 만들어야합니다. 자신에 따라 위의 코드를 시도하고 수정 ..

관련 문제