ios開發(fā)用SDAutoLayout適配UItableView,自定義cell動態(tài)行高

#import <UIKit/UIKit.h>
#import "Header.h"
#import "HeightCell.h"
#import "DataModels.h"
@interface tabViewController : UIViewController
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)commBaseClass *models;
@end

#import "tabViewController.h"
#import "FTIndicator.h"

#import "FTToastIndicator.h"
#import "FTProgressIndicator.h"
#import "FTNotificationIndicator.h"
@interface tabViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_titlesArray;
    
    UITableView *_tableView;
    
}

@end

@implementation tabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    [FTIndicator showProgressWithmessage:@"正在拼命加載中"];

   
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.separatorColor=[UIColor clearColor];
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
    
    [minAFNetworking dataRequestblock:^(NSDictionary *dic) {
      self.models= [[commBaseClass alloc]initWithDictionary:dic];
        if (self.models.code ==200) {
            _tableView.separatorColor=[UIColor grayColor];
            [_tableView reloadData];
        }else{
            NSLog(@"%@",self.models.msg);
        }
         [FTIndicator dismissProgress];//停止轉(zhuǎn)圈
    }];
    // Do any additional setup after loading the view.
}
//返回行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.models.data.communityDynamics!=nil) {
        if (self.models.data.communityDynamics.count>0) {
           
            return self.models.data.communityDynamics.count;
        }
    }
    return 0;
}

//設(shè)置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.models.data.communityDynamics.count>0) {
        commCommunityDynamics *dic=self.models.data.communityDynamics[indexPath.row];
        /* model 為模型實例, keyPath 為 model 的屬性名缕棵,通過 kvc 統(tǒng)一賦值接口 */
        return [tableView cellHeightForIndexPath:indexPath model:dic keyPath:@"model" cellClass:[HeightCell class] contentViewWidth:self.view.frame.size.width];
    }
    return 0;
}
//設(shè)置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HeightCell *cell=[[HeightCell alloc]init];
    
    cell.model=self.models.data.communityDynamics[indexPath.row];
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    return cell;
}
//點擊cell執(zhí)行該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//反選
}

#import <UIKit/UIKit.h>
#import "Header.h"
@interface HeightCell : UITableViewCell
@property(nonatomic,strong)commCommunityDynamics *model;
@end

#import "HeightCell.h"
#import "Header.h"
#import "PicView.h"
@implementation HeightCell
{
    UILabel *title;
    UILabel *time;
    UIImageView *headrimg;
    UIImageView *image;
    UILabel *context;
    UIImageView *typeImg;

    PicView *_picView;
    

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        headrimg=[[UIImageView alloc]init];
       
        [self.contentView addSubview:headrimg];
        
        title=[[UILabel alloc]init];
        title.font=[UIFont systemFontOfSize:12];
        title.textColor=[self colorWithHexString:@"#282828"];
        [self.contentView addSubview:title];
        
        
        time=[UILabel new];
        time.font=[UIFont systemFontOfSize:10];
        time.textColor=[self colorWithHexString:@"#282828"];
        [self.contentView addSubview:time];
        
        
        typeImg=[UIImageView new];
        [self.contentView addSubview:typeImg];
        
        context=[UILabel new];
        context.numberOfLines=0;
        context.font=[UIFont systemFontOfSize:12];
        context.textColor=[self colorWithHexString:@"#282828"];
        [self.contentView addSubview:context];
        
        image=[UIImageView new];
        //設(shè)置圖片不變形剪切出最適合的一段
        image.contentMode =  UIViewContentModeScaleAspectFill;
        image.clipsToBounds  = YES;
        [self.contentView addSubview:image];
       //
        headrimg.sd_layout.topSpaceToView(self.contentView,20).leftSpaceToView(self.contentView,14).widthIs(40).heightIs(40);
         headrimg.layer.cornerRadius = 20;
        headrimg.layer.masksToBounds = 20;
        
        typeImg.sd_layout.topSpaceToView(self.contentView,20).rightSpaceToView(self.contentView,20).widthIs(51).heightIs(15);
        
        context.sd_layout.leftSpaceToView(self.contentView,14).topSpaceToView(self.contentView,74).rightSpaceToView(self.contentView,14);
        
        _picView=[PicView new];
       
        [self.contentView addSubview:_picView];
        _picView.sd_layout.leftSpaceToView(self.contentView,0).rightSpaceToView(self.contentView,0).heightIs(0);
  
         
      
        
    }

    return self;
}
-(void)setModel:(commCommunityDynamics *)model{
    [headrimg sd_setImageWithURL:[NSURL URLWithString:model.icon]];

    title.text=model.nickname;
    title.sd_layout.topEqualToView(headrimg).leftSpaceToView(headrimg,15).heightIs(13).rightSpaceToView(self.contentView,70);
    time.text=[NSString stringWithFormat:@"%@  來自%@",model.createdAt,model.communityName];
    time.sd_layout.leftEqualToView(title).topSpaceToView(title,7).heightIs(10).rightSpaceToView(self.contentView,70);
    NSInteger typIdx=model.tagType;
    switch (typIdx) {
        case 1:
        {
            //閑置轉(zhuǎn)賣
            typeImg.image=[UIImage imageNamed:@"release_2handspare"];
        }
            break;
        case 2:
        {
            //曬物分享
            typeImg.image=[UIImage imageNamed:@"release_share"];
        }
            break;
        case 3:
        {//小聚會
            typeImg.image=[UIImage imageNamed:@"release_party"];
        }
            break;
        case 4:
        {//來拼單
            typeImg.image=[UIImage imageNamed:@"release_pindan"];
        }
            break;
        case 5:
        {//幫幫忙
            typeImg.image=[UIImage imageNamed:@"release_help"];
        }
            break;
        case 6:
        {//熱門活動
            typeImg.image=[UIImage imageNamed:@"熱門活動"];
        }
            break;
        case 7:
        {//社區(qū)資訊
            typeImg.image=[UIImage imageNamed:@"社區(qū)資訊"];
        }
            break;
            
        default:
            break;
    }
    context.text=model.content;
    context.sd_layout.autoHeightRatio(0);
    

    if(model.images==nil||model.images.count<1){
          [self setupAutoHeightWithBottomView:context bottomMargin:20];
    }else if(model.images.count==1){
        if (![model.images[0] isEqual:[NSNull null]]) {
            [image sd_setImageWithURL:[NSURL URLWithString:model.images[0]] placeholderImage:nil];
            image.sd_layout
            .topSpaceToView(context, 15)
            .leftEqualToView(context)
            .widthIs(255).heightIs(135);
            [self setupAutoHeightWithBottomView:image bottomMargin:20];
        }else{
         [self setupAutoHeightWithBottomView:context bottomMargin:20];
        }
       
    }else if(model.images.count>1){
        // _picView.backgroundColor=[UIColor redColor];
        _picView.sd_layout.topSpaceToView(context,0);
        _picView.array=model.images;
       [self setupAutoHeightWithBottomView:_picView bottomMargin:0];
    }


}
//根據(jù)UI設(shè)計師給的顏色值返回出UIColor
-(UIColor *) colorWithHexString: (NSString *)color{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString length] <6) {
        return [UIColor clearColor];
    }
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] !=6)
        return [UIColor clearColor];
    NSRange range;
    range.location =0;
    range.length =2;
    NSString *rString = [cString substringWithRange:range];
    range.location =2;
    NSString *gString = [cString substringWithRange:range];
    range.location =4;
    NSString *bString = [cString substringWithRange:range];
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r /255.0f) green:((float) g /255.0f) blue:((float) b /255.0f) alpha:1.0f];
}

#import <UIKit/UIKit.h>

@interface PicView : UIView
@property(nonatomic,strong)NSArray *array;
@end

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
    }
    return self;
}

-(void)setArray:(NSArray *)array{
    NSLog(@"%@",array);

    
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < array.count; i++) {
        UIImageView *img=[UIImageView new];
        [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",array[i]]]];
        //設(shè)置圖片不變形剪切出最適合的一段
        img.contentMode =  UIViewContentModeScaleAspectFill;
        img.clipsToBounds  = YES;
        [self addSubview:img];
        img.sd_layout.autoHeightRatio(1); // 設(shè)置高度約束
        [temp addObject:img];
    }

    // 此步設(shè)置之后_autoWidthViewsContainer的高度可以根據(jù)子view自適應(yīng)
    //第一個參數(shù)每行幾個,第二個參數(shù)子視圖上下間距,第三個參數(shù)子視圖之間左右間距,第四個參數(shù)兩端距離頂部和底部的距離.,第五個參數(shù)兩端距離左右的距離
    [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:17.5 horizontalMargin:17.5 verticalEdgeInset:17.5 horizontalEdgeInset:17.5];


}

效果圖


屏幕快照 2017-01-20 下午3.40.22.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末孵班,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子招驴,更是在濱河造成了極大的恐慌篙程,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件别厘,死亡現(xiàn)場離奇詭異虱饿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)触趴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門氮发,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人雕蔽,你說我怎么就攤上這事折柠”瞿龋” “怎么了批狐?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我嚣艇,道長承冰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任食零,我火速辦了婚禮困乒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘贰谣。我一直安慰自己娜搂,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布吱抚。 她就那樣靜靜地躺著百宇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秘豹。 梳的紋絲不亂的頭發(fā)上携御,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機(jī)與錄音既绕,去河邊找鬼啄刹。 笑死,一個胖子當(dāng)著我的面吹牛凄贩,可吹牛的內(nèi)容都是我干的誓军。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼怎炊,長吁一口氣:“原來是場噩夢啊……” “哼谭企!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起评肆,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤债查,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瓜挽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盹廷,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年久橙,在試婚紗的時候發(fā)現(xiàn)自己被綠了俄占。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡淆衷,死狀恐怖缸榄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祝拯,我是刑警寧澤甚带,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布她肯,位于F島的核電站,受9級特大地震影響鹰贵,放射性物質(zhì)發(fā)生泄漏晴氨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一碉输、第九天 我趴在偏房一處隱蔽的房頂上張望籽前。 院中可真熱鬧,春花似錦敷钾、人聲如沸枝哄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膘格。三九已至,卻和暖如春财松,著一層夾襖步出監(jiān)牢的瞬間瘪贱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工辆毡, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留菜秦,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓舶掖,卻偏偏與公主長得像球昨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子眨攘,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

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