Iphone 当UIImage不存在时如何显示UILabel

Iphone 当UIImage不存在时如何显示UILabel,iphone,ios,uitableview,uilabel,Iphone,Ios,Uitableview,Uilabel,我有一个从服务器加载图像的UITableView。但有时UITableView上没有要显示的图像,因此我希望显示UILabel。想知道我该如何做到这一点。我将感谢任何帮助或代码片段来实现这一点。 多谢各位 我试过你说的了。第一次加载表格时,一切正常,但一旦开始滚动所有标签和按钮,所有位置都会显示出来。 这是我的密码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowA

我有一个从服务器加载图像的UITableView。但有时UITableView上没有要显示的图像,因此我希望显示UILabel。想知道我该如何做到这一点。我将感谢任何帮助或代码片段来实现这一点。 多谢各位

我试过你说的了。第一次加载表格时,一切正常,但一旦开始滚动所有标签和按钮,所有位置都会显示出来。
这是我的密码

- (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;

                }

cellforrowatinexpath
方法中

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]

我可以看到问题所在,并可以告诉您解决此问题的正确步骤

    - (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;
}
您需要使用动态内容视图创建单元格。
尝试上面的代码段,并根据您自己的修改..

如果(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];},我是否应该将其包含在其中或者将其置于循环之外?这将是
cellForRowAtIndexPath
方法的开始。您不应该调用
cellForRowAtIndexPath
方法获取图像,请在其他地方抓取图像,然后重新加载tableView。我们将尝试一下。谢谢你抽出时间。