可重复使用的UITableviewcell无法正确显示初始图像

时间:2014-10-16 04:04:31

标签: ios objective-c uitableview ios8 cliptobounds

所以我有一个动态原型的UITableview。 单元格是具有4个属性的自定义单元格:

  • imageVBkg(背景图片)
  • mainText(label)
  • 副标题(标签)
  • personImageView(uiimageview with the person's photo)。

我在原版故事板中工作得很好,但最近搬到了新的故事板。 (仅供参考我无法使用旧故事板重新测试,因为代码中有一些更改会排除这种情况。)

**这个看起来像是自动出现问题。禁用AUTOLAYOUT会修复问题(但自动退出是出于原因)。 **

基本上,问题是:

  • 标签内容显示正常(数据来自fetchedresultscontroller / coredata)
  • imageVBkg正常
  • 记录图像确实存在的报告
  • 列表项
  • 最初显示时,personImageView内容不可见
  • 一旦我滚动并且"重复使用"单元格,personImageView内容正确显示。

**编辑**

  • 如果我选择一个单元格并转到下一个屏幕,然后点击保存按钮,应用程序将返回原始tableview屏幕,并显示图像。更新coredata信息会使图像显现出来并不是真的有意义。

下面的一些示例代码......

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
    }
    cell.delegate = self;
    cell.dataSource = self;
    cell.backgroundColor = _blueColor;

    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

-(void)configureCell:(PersonCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

    Person *person = [_fetchedResultsController objectAtIndexPath:indexPath];

    cell.imageVBkg.image = [[UIImage imageNamed:@"list-item-background"] resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 30, 30)];
    cell.mainText.text = [NSString stringWithFormat:@"%@ %@", person.firstname, person.lastname];
    Company *company = (Company *)person.worksFor;
    NSString *fullString = person.position;
    if ([company.name length] > 0 && [person.position length] > 0) {
        fullString = [fullString stringByAppendingString:@" at "];
    }
    if (company.name != nil) {
        fullString = [fullString stringByAppendingString:company.name];
    }

    cell.subtitle.text = fullString;

    if ([person hasPhoto]) {
        _image = [person photoImage];
        if (_image != nil) {
            cell.personImageView.clipsToBounds = YES;
            cell.personImageView.image = _image;
            cell.personImageView.layer.cornerRadius = cell.personImageView.bounds.size.width / 2.0f;
        }
    }
}

PersonCell.h具有以下属性:

@property (nonatomic, weak) IBOutlet UILabel *mainText;
@property (nonatomic, weak) IBOutlet UILabel *subtitle;
@property (nonatomic, weak) IBOutlet UIImageView *personImageView;
@property (strong, nonatomic) IBOutlet UIImageView *imageVBkg;

另外值得一提...... 如果我不使用" cell.personImageView.clipsToBounds = YES;"那么personImageView图像会出现,但是作为一个正方形,而不是一个圆形,这就是我需要它的方式。

希望这是一个简单的修复......

提前感谢任何建议。

3 个答案:

答案 0 :(得分:1)

在此处的单元格中使用此代码122,111是表格视图单元格中的图像视图的框架

 CGSize itemSize = CGSizeMake(122,111);    
 UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);           
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);     
    [cell.imageView.image drawInRect:imageRect];             
  cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();      
    UIGraphicsEndImageContext();

答案 1 :(得分:0)

首先,您需要标记您的手机。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
    }
    cell.delegate = self;
    cell.dataSource = self;
    cell.backgroundColor = _blueColor;

   //See these two lines 
   //Edit
    cell.tag = indexPath.row;

   [self configureCell:cell];

   return cell;
}

第二,你必须改变你的配置单元格方法。

  -(void)configureCell:(PersonCell *)cell
  {
   //Edit
    NSInteger index = (NSInteger) (cell.tag)
    NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];

    // after this place your code.
   }

答案 2 :(得分:0)

完全禁用自动布局和大小类,然后再次重新启用和配置它们已经由于某种原因修复了它。只是删除约束不起作用。 不知道真正的问题是什么。建议欢迎。