zoukankan      html  css  js  c++  java
  • 自定义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

     
  • 相关阅读:
    C# 控制台程序如何输出Messagebox
    C# VS如何整个项目中查找字符串
    C# .NET using ManagementObjectSearcher提示缺少引用怎么办
    [React] Write a Custom State Hook in React
    [GCP] Goolge compute Engine
    [Kubernetes] Defining a Pod with YAML
    [Kubernetes] Kubectl and Pod
    [Schematics] 1. Copy and Manipulate Template
    [Schematics] 0. Schematics "Hello World"
    [RxJS] Subject asObservable() method
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4237707.html
Copyright © 2011-2022 走看看