如果你在寻找一个UITableView加载远程图片的方法,推荐一个第三方类库:SDWebImage。这个类库为你的工程提供了灵活的加载&缓存网络图片的方法,而且非常容易在工程中使用。
但当我开始使用时,当我加载不同分辨率的网络图片时,发现图片出现了不同宽度,很不美观,为了解决这个问题,我们需要对webimage中的不同宽度进行限制。
下面是一个简便有效的处理方法,自定义layoutSubviews:
1 - (void)layoutSubviews { 2 [super layoutSubviews]; 3 self.imageView.frame = CGRectMake(5,5,40,32.5); 4 float limgW = self.imageView.image.size.width; 5 if(limgW > 0) { 6 self.textLabel.frame = CGRectMake(55,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height); 7 self.detailTextLabel.frame = CGRectMake(55,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width,self.detailTextLabel.frame.size.height); 8 } 9 }
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 static NSString *CellIdentifier = @"Cell"; 4 5 MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 6 if (cell == nil) { 7 cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 8 } 9 10 // Configure the cell... 11 [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 12 placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 13 return cell; 14 }