學伴分析(二)UITableView優化

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

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

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

@interface TableViewController ()
@property (nonatomic, strong) NSMutableArray *heightMArray;/** cell高度數組*/
@property (nonatomic, strong) NSMutableArray *newsMArray;  /** 新聞數據源*/
@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])];
    //...獲取網絡數據, 在這里用假數據
    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用來判斷是否已經update高度數組中圖片的高度,如果沒有if會陷入死循環
        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地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,665評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,627評論 2 380

推薦閱讀更多精彩內容