我想在SDWebImage
下載完成時調整tableviewcell
的大小。如何在下載圖像完成後更改UITableViewCell高度?
我應該在哪裏放[UITableViewCell setNeedsUpdateConstraints]
?
或者只是發送通知到UIViewController
並重新加載tableview數據?
ViewController.m
#import "ViewController.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
#define WEAKSELF typeof(self) __weak weakSelf = self;
@interface ViewController()
<UITableViewDataSource, UITableViewDelegate> {
UITableView *myTableView;
}
@property (nonatomic, retain) NSArray *items;
@end
@implementation ViewController
- (UIRectEdge)edgesForExtendedLayout {
return UIRectEdgeNone;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadRows:)
name:@"ReloadRows"
object:nil];
NSMutableArray *array = [NSMutableArray array];
for (NSInteger i = 0; i < 30; i ++) {
CGFloat width = arc4random() % 100 + 20.0f;
CGFloat height = arc4random() % 100 + 20.0f;
CGSize size = CGSizeMake(width, height);
[array addObject:[NSValue valueWithCGSize:size]];
}
self.items = array;
myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
myTableView.estimatedRowHeight = 60.0f;
myTableView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:myTableView];
UIView *superview = self.view;
[myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.equalTo(superview);
}];
}
- (void)reloadRows:(NSNotification *)notification {
NSIndexPath *ip = notification.object;
if (ip) {
TestCell *cell = [myTableView cellForRowAtIndexPath:ip];
[cell setNeedsUpdateConstraints];
[myTableView reloadRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"Contact_People_Cell";
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSValue *value = [self.items objectAtIndex:indexPath.row];
cell.imageSize = [value CGSizeValue];
[cell refreshInterface];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end
TestCell
#define SIZE_DEFAULT CGSizeMake(140.0f, 100.0f)
@interface TestCell() {
UIImageView *imv;
}
@end
@implementation TestCell
@synthesize imageSize;
@synthesize indexPath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setupView];
}
return self;
}
- (void)setupView {
imv = [[UIImageView alloc] initWithFrame:CGRectZero];
imv.backgroundColor = [UIColor darkGrayColor];
[self.contentView addSubview:imv];
self.translatesAutoresizingMaskIntoConstraints = NO;
UIView *superView = self.contentView;
[imv mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(SIZE_DEFAULT.width));
make.height.equalTo(@(SIZE_DEFAULT.height));
make.left.top.bottom.equalTo(superView).with.offset(10.0f);
}];
}
- (void)updateConstraints {
[super updateConstraints];
UIView *superView = self.contentView;
[imv mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(superView).with.offset(10.0f);
make.bottom.equalTo(superView).with.offset(-10.0f);
make.width.equalTo(@(ceilf(self.imageSize.width)));
make.height.equalTo(@(ceilf(self.imageSize.height)));
}];
}
- (void)refreshInterface {
WEAKSELF
[imv sd_setImageWithURL:[NSURL URLWithString:@"http://www.baidu.com"]
placeholderImage:nil
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
imv.backgroundColor = [UIColor purpleColor];
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat width = arc4random() % 100 + 140.0f;
CGFloat height = arc4random() % 100 + 140.0f;
CGSize size = CGSizeMake(width, height);
weakSelf.imageSize = size;
[weakSelf setNeedsUpdateConstraints];
// NSIndexPath *ip = [weakSelf.indexPath copy];
// [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadRows" object:ip];
});
}];
}
@end
參閱本。 http://stackoverflow.com/a/13105218/4030971 –