• 自定义cell的高度


    //
    //  RootTableViewController.m
    //  Share
    //
    //  Created by lanouhn on 15/1/20.
    //  Copyright (c) 2015 niutiantian. All rights reserved.
    //

    #import "RootTableViewController.h"
    #import "CustomTableViewCell.h"
    #import "DetailViewController.h"

    static NSString *cellIndentifer = @"cell";

    @interface RootTableViewController ()

    @end

    @implementation RootTableViewController

    - (void)dealloc
    {
        self.charString = nil;
        self.secondString = nil;
        self.custom = nil;
        [super dealloc];
    }

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.charString = @"只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?";
        self.secondString = @"只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸";
        
            //注册cell
        [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:cellIndentifer];
        self.custom = [self.tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    }

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

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return 10;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer forIndexPath:indexPath];
        if (indexPath.row % 2 == 0) {
            cell.label.text = _charString;         [cell resetLabelFram:_charString];              } else {         cell.label.text = _secondString;         [cell resetLabelFram:_secondString];     }         //错误案例 //    if (indexPath.row % 2 == 0) { //        self.tableView.rowHeight = [CustomTableViewCell heigehtCharString:_charString]; //    } else { //        self.tableView.rowHeight = [CustomTableViewCell heigehtCharString:_secondString]; //    }     return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {         //方法一 //    if (indexPath.row % 2 == 0) { //       return [CustomTableViewCell heigehtCharString:_charString]; //    } else { //        return [CustomTableViewCell heigehtCharString:_secondString]; //    }         //方法二     if (indexPath.row % 2 == 0) {         self.custom.label.text = _charString;             } else {         self.custom.label.text = _secondString;          }     CGRect rect = [self.custom.label.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];     return rect.size.height; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     DetailViewController *detail = [[DetailViewController alloc] init];     [self.navigationController pushViewController:detail animated:YES];     [detail release]; } @end // //  CustomTableViewCell.m //  Share // //  Created by lanouhn on 15/1/20. //  Copyright (c) 2015年 niutiantian. All rights reserved. // #import "CustomTableViewCell.h" @implementation CustomTableViewCell -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {         self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];         _label.numberOfLines = 0;         [self addSubview:_label];         [_label release];     }     return self; } + (CGFloat)heigehtCharString: (NSString *)aString {     CGRect rect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];     return  rect.size.height;      }     //重设label的高度 - (void)resetLabelFram: (NSString *)aString {        CGRect rect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];          _label.frame = CGRectMake(0, 0, self.bounds.size.width, rect.size.height); } @end

     
  • 相关阅读:
    H3C防火墙/路由器通过Track实现双线接入
    为了安装runlike 升级python2至python3
    URL:windows_exporter-0.13.0-amd64
    ES_Start
    luogu4323 独特的树叶
    luogu5043
    java操作Jacoco合并dump文件
    【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
    【Azure 应用服务】App Service下部署的应用报错 Out of Memory
    【Azure Developer】使用Key Vault的过程中遇见的AAD 认证错误
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4237707.html
Copyright © 2020-2023  润新知