0

我有一个 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;

                }
4

2 回答 2

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

您需要使用动态内容视图创建单元格。 尝试上面的代码片段,并根据自己的修改..

于 2013-07-26T12:26:52.237 回答
0

在你的cellForRowAtIndexPath方法中

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

于 2013-07-24T15:19:33.003 回答