【问题标题】:Image not loading in tableview in iOS 10 on first time, but after scroll?图像第一次未在 iOS 10 的 tableview 中加载,但在滚动后?
【发布时间】:2017-03-09 23:31:21
【问题描述】:

我在 iOS 10 中的 tableview 中显示图像时遇到问题,但相同的代码在 iOS 10 下工作,我在下面添加了我的代码的 sn-p。请帮忙。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"InboxTableViewCell";

InboxTableViewCell *cell = (InboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
    cell = [[InboxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell setBackgroundColor:[cellColors objectAtIndex:indexPath.row]];
[cell.profileImageView setImage:[UIImage imageNamed:@"profile.jpg"]];

return cell;
}

在单元格中,我正在制作圆形图像

 #import "InboxTableViewCell.h"
@implementation InboxTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    // Initialization code


}

-(void)layoutSubviews{
    self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width/2;
    self.profileImageView.clipsToBounds=YES;
    [super layoutSubviews];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

【问题讨论】:

  • 圆角半径代码有问题吗?我敢打赌,如果您删除它,您的问题仍然存在,所以我们很可能不需要它来回答。此外,一旦视图从 nib 唤醒就足够了,您不需要重复设置图层属性。
  • dequeueReusableCellWithIdentifier:cellIdentifier 永远不会产生一个 nil 单元格,那么为什么要检查它呢?
  • 还有layOutSubviews 被多次调用。因此,请在cellForRowAtIndexPath 中转圈。
  • 或者最好是- awakeFromNib。从软件设计的角度来看,视图会自行配置。把它放在 viewController 中只是糟糕的设计。
  • 尝试将您的单元格 setImage 方法放入主队列。像这样dispatch_async(dispatch_get_main_queue(), ^(void){ [cell.profileImageView setImage:[UIImage imageNamed:@"profile.jpg"]]; });

标签: ios objective-c uitableview ios10


【解决方案1】:

终于我得到了答案,我现在是我自己的问题。 我们只需要在 drawRect 方法中编写它就可以了。

快乐编码...

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    self.profileImageView.layer.cornerRadius=_profileImageView.frame.size.width/2;
    [_profileImageView setClipsToBounds:YES];
    [_profileImageView layoutIfNeeded];
    [_profileImageView setNeedsDisplay];
}

【讨论】:

    【解决方案2】:

    在 InboxTableViewCell 的 -(void)layoutSubviews 方法中,在设置cornerRadius 之前调用 [super layoutSubviews] .. 在应用布局之前进行设置,因此 [super layoutSubviews] 之前的图像帧将为 1000,因此您将cornerradius 设置为 500。 应用自动布局后,图像尺寸变小,小图像上的 500 角半径将隐藏图像。

    -(void)layoutSubviews{
        [super layoutSubviews];
        self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width/2;
        self.profileImageView.clipsToBounds=YES;
    
    }
    

    【讨论】:

      【解决方案3】:
      [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
      

      【讨论】:

      • 下载这个添加到你的项目并导入
      • 是的,我也试过了,但没用,我只是想设置本地图像。谢谢
      • #import
      • 没有IBOUTLET
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多