2016-08-14 1 views
0

Я хочу изменить размер tableviewcell, когда SDWebImage скачать готовый.Как изменить высоту UITableViewCell при завершении загрузки изображения?

Куда следует положить [UITableViewCell setNeedsUpdateConstraints]?
Или просто отправьте уведомление на номер UIViewController и перезагрузите данные таблицы?

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 
+0

относится к этому. http://stackoverflow.com/a/13105218/4030971 –

ответ

0

Используйте самостоятельно размером ячеек. После загрузки изображения, изменение ограничения высоты UIImage и называть reloadRowsAtIndexPaths: методом

0

осуществить tableView heightForRowAtIndexPath: метод, чтобы изменить высоту ячейки в соответствии с загруженной высотой изображения

Смежные вопросы