學伴分析(二)UITableView優(yōu)化

在這里主要處理這樣一種情況:
后臺請求數(shù)據(jù)返回后瞎颗,在cell上顯示一張與手機屏幕等寬的圖片魄藕,高度根據(jù)比例換算,但是圖片大小后臺是不給的寄啼。也就是說圖片的大小只能等圖片異步加載完后才能確定,進而才能確定cell的高度

思路:
1.給圖片默認的高度代箭,同時用一個可變數(shù)組來管理這些圖片的高度
2.等圖片異步加載完后計算圖片的高度存入數(shù)組中墩划,同時在主線程中重新加載tableView

#import "TableViewController.h"
#import "DBTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIColor+expanded.h"

@interface TableViewController ()
@property (nonatomic, strong) NSMutableArray *heightMArray;/** cell高度數(shù)組*/
@property (nonatomic, strong) NSMutableArray *newsMArray;  /** 新聞數(shù)據(jù)源*/
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[DBTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DBTableViewCell class])];
    //...獲取網(wǎng)絡(luò)數(shù)據(jù), 在這里用假數(shù)據(jù)
    self.newsMArray = [[NSMutableArray alloc] initWithObjects:
@"http://pic2.zhimg.com/0e7f5548fca3cb7c29d69189c609f66d.jpg",
@"http://i0.hdslb.com/bfs/archive/b947b06dfb53af2bda5a53d145934596d5abbab5.jpg",
@"http://pic1.zhimg.com/3a0b66018f28365e57cc51816cb17678.jpg",
@"http://i0.hdslb.com/bfs/archive/c0f647ee5d5d77263fdbc49129563004e40b704e.jpg",
@"http://pic1.zhimg.com/e1ac4a1d20226abb980244537bf537f8.jpg",
@"http://i0.hdslb.com/bfs/archive/9b7e61ceb18aed36138e7726afcebbf69b6862e6.jpg",
@"http://pic2.zhimg.com/5e2f62618ca4bdd8fe4c38ed1ed69271.jpg",
@"http://i0.hdslb.com/bfs/archive/232dd1d0e577b9830386f7a3237de4a3e837c021.jpg",
@"http://pic3.zhimg.com/9e9c66ff2242006a573d37334b35b746.jpg",
@"http://i0.hdslb.com/bfs/archive/ced5cbdd59ce9b751a60472181faf3a6b2b73de8.jpg",
@"http://pic3.zhimg.com/9e9c66ff2242006a573d37334b35b746.jpg",
@"http://i0.hdslb.com/bfs/archive/0c819a923a0e816961ea8d5483584b4b503827c8.jpg",
@"http://pic3.zhimg.com/9caa7d6525ed9df0b505dffcf96a67c6.jpg", nil];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSourse
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.newsMArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DBTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DBTableViewCell class])];
    cell.imageViewHeight = [self.heightMArray objectAtIndex:indexPath.section];
    [cell configWithDict:[self.newsMArray objectAtIndex:indexPath.section]];
    [cell.newsImageView sd_setImageWithURL:[NSURL URLWithString:[self.newsMArray objectAtIndex:indexPath.section]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // ------if用來判斷是否已經(jīng)update高度數(shù)組中圖片的高度,如果沒有if會陷入死循環(huán)
        if ([[self.heightMArray objectAtIndex:indexPath.section] isEqual:@(self.view.frame.size.width*3/5)]) {
            NSNumber *number = [NSNumber numberWithFloat:(image.size.height/image.size.width) * self.view.frame.size.width];
            [self.heightMArray replaceObjectAtIndex:indexPath.section withObject:number];
            // ------在主線程中重新加載tableView
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ------圖片距離cell頂部5+底部5
    return 10 + [[self.heightMArray objectAtIndex:indexPath.section] floatValue];
}

#pragma mark - getter and setter
- (NSMutableArray *)heightMArray {
    if (_heightMArray == nil) {
        _heightMArray = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < self.newsMArray.count; i++) {
            // ------給圖片一個默認高度
            [_heightMArray addObject:@(self.view.frame.size.width*3/5)];
        }
    }
    return _heightMArray;
}
@end

完整版demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嗡综,一起剝皮案震驚了整個濱河市乙帮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌极景,老刑警劉巖察净,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異戴陡,居然都是意外死亡塞绿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門恤批,熙熙樓的掌柜王于貴愁眉苦臉地迎上來异吻,“玉大人,你說我怎么就攤上這事喜庞【骼耍” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵延都,是天一觀的道長雷猪。 經(jīng)常有香客問我,道長晰房,這世上最難降的妖魔是什么求摇? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任射沟,我火速辦了婚禮,結(jié)果婚禮上与境,老公的妹妹穿的比我還像新娘验夯。我一直安慰自己,他們只是感情好摔刁,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布挥转。 她就那樣靜靜地躺著,像睡著了一般共屈。 火紅的嫁衣襯著肌膚如雪绑谣。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天拗引,我揣著相機與錄音借宵,去河邊找鬼。 笑死寺擂,一個胖子當著我的面吹牛暇务,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播怔软,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼择镇!你這毒婦竟也來了挡逼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤腻豌,失蹤者是張志新(化名)和其女友劉穎家坎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吝梅,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡虱疏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了苏携。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片做瞪。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖右冻,靈堂內(nèi)的尸體忽然破棺而出装蓬,到底是詐尸還是另有隱情,我是刑警寧澤纱扭,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布牍帚,位于F島的核電站,受9級特大地震影響乳蛾,放射性物質(zhì)發(fā)生泄漏暗赶。R本人自食惡果不足惜鄙币,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蹂随。 院中可真熱鬧十嘿,春花似錦、人聲如沸糙及。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浸锨。三九已至唇聘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間柱搜,已是汗流浹背迟郎。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留聪蘸,地道東北人宪肖。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像健爬,于是被迫代替她去往敵國和親控乾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容