cell高度自適應
阿新 • • 發佈:2018-12-17
在這裡插入程式碼片#import "JHHViewController.h" #import "UITableView+SDAutoTableViewCellHeight.h" #import "JHHTableViewCell.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface JHHViewController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView * tableView; @property(nonatomic,strong) NSArray * dataSource; @end @implementation JHHViewController - (void)viewDidLoad { [super viewDidLoad]; [self setNav]; self.dataSource = @[@"毛澤東(1893年12月26日-1976年9月9日),字潤之(原作詠芝,後改潤芝),筆名子任。湖南湘潭人。中國人民的領袖,馬克思主義者,偉大的無產階級革命家、戰略家和理論家,中國共產黨、中國人民解放軍和中華人民共和國的主要締造者和領導人,詩人,書法家。",@"1949至1976年,毛澤東擔任中華人民共和國最高領導人。他對馬克思列寧主義的發展、軍事理論的貢獻以及對共產黨的理論貢獻被稱為毛澤東思想。因毛澤東擔任過的主要職務幾乎全部稱為主席,所以也被人們尊稱為“毛主席”。",@"毛澤東,湖南湘潭人。1893年12月26日生於一個農民家庭。辛亥革命爆發後在起義的新軍中當了半年兵。1914~1918年,在湖南第一師範學校求學。畢業前夕和蔡和森等組織革命團體新民學會。五四運動前後接觸和接受馬克思主義,1920年11月,在湖南建立共產主義組織。1921年7月,出席中國共產黨第一次全國代表大會,後任中共湘區委員會書記,領導長沙、安源等地工人運動。1923年6月,出席中共“三大”,被選為中央執行委員,參加中央領導工作。1924年1月國共合作後,在國民黨第一、第二次全國代表大會上都當選為候補中央執行委員,曾在廣州任國民黨中央宣傳部代理部長,主編《政治週報》,主辦第六屆農民運動講習所。1926年11月,任中共中央農民運動委員會書記。",@"1925年冬至1927年春,先後發表《中國社會各階級的分析》、《湖南農民運動考察報告》等著作,指出農民問題在中國革命中的重要地位和無產階級領導農民鬥爭的極端重要性,批評了陳獨秀的右傾思想。",@"抗日戰爭勝利後,針對蔣介石企圖消滅共產黨及其武裝力量的現實,他提出“針鋒相對”的鬥爭方針。",@"1946年夏蔣介石發動全面內戰後,毛澤東同朱德、周恩來領導中國人民解放軍進行積極防禦,集中優勢兵力,各個殲滅敵人。1947年3月至1948年3月,同周恩來、任弼時轉戰陝北,指揮西北戰場和全國的解放戰爭。1947年夏,中國人民解放軍從戰略防禦轉入戰略進攻,在以他為首的黨中央領導下,經過遼瀋、淮海、平津三大戰役和1949年4月的渡江戰役,推翻了國民黨政府。1949年3月,主持召開中共七屆二中全會,並作重要報告,決定把黨的工作重心從農村轉到城市,規定了黨在全國勝利以後的各項基本政策,號召全黨務必保持謙虛、謹慎、不驕、不躁的作風,務必繼續保持艱苦奮鬥的作風。7月1日,發表《論人民民主專政》,規定了人民共和國的政權的性質及其對內對外的基本政策。"]; [self.view addSubview:self.tableView]; } #pragma maek -- 設定導航條 -(void)setNav{ self.navigationController.navigationBar.barStyle = UIBarStyleBlack; self.title = @"JHH-SDAutoLayout"; self.navigationController.navigationBar.translucent = NO; } #pragma mark -- 懶載入建立TableView -(UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain]; _tableView.dataSource =self; _tableView.delegate = self; [_tableView registerClass:[JHHTableViewCell class] forCellReuseIdentifier:@"cell"]; } return _tableView; } #pragma mark -- 表格協議方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataSource.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ JHHTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.contentLabel.text = _dataSource[indexPath.row]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //一句話搞定cell高度自適應 return [self cellHeightForIndexPath:indexPath cellContentViewWidth:WIDTH tableView:tableView]; } @end
@property(nonatomic,strong) UIImageView *iconImageV; @property(nonatomic,strong) UILabel * titleLabel; @property(nonatomic,strong) UILabel * contentLabel; #import "JHHTableViewCell.h" #import "SDAutoLayout.h" @implementation JHHTableViewCell -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle: style reuseIdentifier:reuseIdentifier]; if (self) { self.iconImageV = [[UIImageView alloc]init]; self.iconImageV.image = [UIImage imageNamed:@"1"]; self.titleLabel = [[UILabel alloc]init]; self.titleLabel.text = @"我是標題"; self.contentLabel = [[UILabel alloc]init]; [self.contentView addSubview:self.iconImageV]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.contentLabel]; self.iconImageV.sd_layout .widthIs(50) .heightIs(50) .topSpaceToView(self.contentView, 10) .leftSpaceToView(self.contentView, 10); self.titleLabel.sd_layout .topSpaceToView(self.contentView, 10) .leftSpaceToView(self.iconImageV, 10) .rightSpaceToView(self.contentView, 10) .heightIs(20); self.contentLabel.sd_layout .leftSpaceToView(self.iconImageV, 8) .topSpaceToView(self.titleLabel, 5) .rightSpaceToView(self.contentView, 10) .autoHeightRatio(0); [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:10]; } return self; } @end