【问题标题】:Adding a UIImageview to only some custom cells in a uitableview仅将 UIImageview 添加到 uitableview 中的一些自定义单元格
【发布时间】:2014-01-12 21:29:50
【问题描述】:

我试图将 UIImage 仅添加到 UITableview 中的某些单元格,但是每当用户滚动经过图像时,我最终会得到一堆放错位置的图像视图,图像视图最终会出现在所有错误的单元格中,这只是一个混乱。感谢您提前回复。 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"sections:%ld", (long)tableView.numberOfSections);
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell;

      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *message =  self.retreivedMessages[indexPath.row];
    NSLog(@"%lu", (long)indexPath.row);
    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:20000];
    UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:10000];
    UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:30000];
    image.tag = indexPath.row;
    UILabel *usernameLabel = (UILabel *)[cell.contentView viewWithTag:50000];
    UILabel *dateLabel = (UILabel *)[cell.contentView viewWithTag:60000];

    UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedPhoto:)];
    CGRect imageFrame = image.frame;
    CGPoint imagePoint = imageFrame.origin;
    imagePoint.y = 8;
    image.frame = imageFrame;
    NSLog(@"frameY: %f", imageFrame.origin.y);
    NSLog(@"imageFrameOrigin: %f", image.frame.origin.y);
    UIButton *replyButton = (UIButton *)[cell.contentView viewWithTag:40000];
    replyButton.tag = indexPath.row;

    messageContent.text = [message objectForKey:@"messageContent"];
    NSString *content = [message objectForKey:@"messageContent"];
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]};
    CGRect rect = [messageContent.text boundingRectWithSize:CGSizeMake(251, MAXFLOAT)
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                 attributes:attributes
                                                    context:nil];


    messageContent.font = [UIFont systemFontOfSize:17.0f];
    CGFloat height = ceilf(rect.size.height);

    messageContent.frame = CGRectMake(40, 100, 251, height);
    messageContent.lineBreakMode = NSLineBreakByWordWrapping;
    NSLog(@"Message: %@", content);
    UIImageView *photoView = (UIImageView *)[cell.contentView viewWithTag:70000];


    NSLog(@"boolean photo: %@", [message objectForKey:@"hasPhoto"]);
    if ([[message objectForKey:@"hasPhoto"] isEqualToString:@"YES"]) {

        PFFile *photoFile = [message objectForKey:@"photo"];
    [self addPhotoToCell:cell file:photoFile photoView:photoView];
     [photoView addGestureRecognizer:tapPhoto];

        NSLog(@"added");
    } else {

    }

    NSLog(@"Height:%f", messageContent.frame.size.height);
    NSLog(@"y: %f", messageContent.frame.origin.y);
    NSLog(@"estimated height: %f", height);
    NSLog(@"imageY: %f", image.frame.origin.y);
    NSLog(@"ID: %@", [message objectForKey:@"ID"]);

    usernameLabel.text = [message objectForKey:@"senderSetName"];
    nameLabel.text = [message objectForKey:@"senderName"];
    PFFile *photoProfile = [message objectForKey:@"senderPicture"];
    NSData *data = [photoProfile getData];
    UIImage *profileImage = [UIImage imageWithData:data];
    image.image = profileImage;
    image.layer.cornerRadius = 27.0;
    image.layer.masksToBounds = YES;

    self.messageDate = [message objectForKey:@"date"];
    NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
    [formatDate setDateFormat:@"MM/dd 'at' hh:mm a"];
    NSString *dateString = [formatDate stringFromDate:self.messageDate];
    dateLabel.text = dateString;

    return cell;
}

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview uiimageview


    【解决方案1】:

    如果您只想在某些情况下出现“照片”视图,那么我认为您想在else 块中添加一些代码以隐藏或删除单元格中的任何照片视图。

    问题在于,一旦您开始滚动,表格视图将开始重用一些已滚动到屏幕外的单元格来填充正在滚动到视图中的新单元格。如果其中一个单元格有一张可见的照片(通过您正在创建照片视图并调用 addPhotoToCellif 语句的真正分支),那么 dequeueReusableCellWithIdentifier: 可能会返回其中一个单元格,其所有视图都完好无损而不是返回一个全新的未初始化单元格。如果您最终将其中一个单元格重用于 hasPhoto 不是 YES 的消息行,那么看起来您没有做任何事情来隐藏该照片视图。因此,向else 块添加一些代码以隐藏该照片视图(如果存在)。

    【讨论】:

    • 我添加了代码以从 else 下的单元格中删除 photoview 但这只是导致只有第一个 tableview 单元格中似乎有所需的图像,而之后的其他单元格没有出现。跨度>
    【解决方案2】:

    删除此行,因为它将重用用户滚动后释放的单元格。假设向上滚动的单元格是否有 UIImageView 并且该单元格可能会被重用(这在您的情况下会导致问题)。

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    而是每次都创建一个单元格。它会很好用。

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    

    如果这会使你的应用程序变慢,请使用 if else 条件检查释放的单元格是否有 cellImage 并将其删除。

    【讨论】:

      【解决方案3】:

      你的方法是错误的。

       -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
      {
           UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"1"];
      
           if (!cell)
           {
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];
               UIImageView *cellImage = [UIImageView.alloc initWithFrame:CGRectMake(10, 5, 35 , 35)];
               cellImage.tag = 1 ;
               [cellImage setBackgroundColor:[UIColor clearColor]];
               [cell.contentView addSubview:cellImage];
      
               [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
           }
      
           UIImageView*cellImage = (UIImageView*)[cell.contentView viewWithTag:1];
           [cellImage setImage:[UIImage imageNamed:@"ratingFullIcon.png"]];
           return cell;    
      }
      

      你应该分配 UITableViewCell : UITableViewCell contentView 中的所有子视图也应在单元格不存在时分配。如果它存在,我们不需要分配初始化。

      setText 或 setImage 超出此条件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        相关资源
        最近更新 更多