iOS 根据文字内容设置cell 的高度

2023-05-16

今天学习一个简单的,根据内容的大小设置cell 的高度.

第一步:建两个类,分别继承于UIViewController 和UITableViewCell .

第二步:

mainViewController.h

#import <UIKit/UIKit.h>


@interface mainViewController : UIViewController


@property (retain, nonatomic) NSMutableArray *array;


第三步:

  mainViewScroller.m

#import "mainViewController.h"

#import "mainTableViewCell.h"


@interface mainViewController ()<UITableViewDataSource, UITableViewDelegate>


@end


@implementation mainViewController


- (void)dealloc{

    [self.array release];

    [super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        self.array = [NSMutableArray array];

     

        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@" 这天是许俊浩和沈睿言在一起交往三周年的日子.\n可是这么美好的日子却掀起了一场战争,没错,许俊浩觉得完全可以用战争来形容这场混乱。\n本来为了度过美好的今天,许俊浩还特地推掉了今天的工作安排。\n岂料因为不满沈睿言沉浸在网络游戏里,许俊浩一时冲动拔掉了网线,所以就这样惹怒了他的小情人沈睿言。\n“你这个混蛋!你怎么可以在我交易物品的时候拔掉我的网线?!沈睿言怒气冲冲一把揪住许俊浩的衣领咆哮着。\n 被沈睿言这么一吼,许俊浩的火气也上来了,他一把甩开沈睿言揪着自己衣领的手,骂道:天天就知道游戏!就算你是职业玩家也用不着这么拼命吧?!”\n“不这样哪有钱!沈睿言马上愤怒的反驳。\n“我难道养不起你吗?!许俊浩说了这么一句之后,准备动手把那些电脑都砸了,这些电脑是你对象吗?!妈的,有没有搞清楚你到底在跟谁交往?!”\n沈睿言被他这样骂,再加上看到他要砸电脑的动作,便立刻动手用力推开了许俊浩。",@"name", nil];       

        [_array addObject:dic];

    }

    return self;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationController.navigationBar.translucent = NO;

    

    [self create];

    

}


- (void) create{

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain];

    table.backgroundColor = [UIColor clearColor];

    table.separatorColor = [UIColor blackColor];

    table.dataSource = self;

    table.delegate = self;

    [self.view addSubview:table];

    [table release];

    

    

    

    

}


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

    return _array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *name = @"ONE";

    mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:name];

    if (!cell) {

        cell = [[mainTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:name];

    }

    NSDictionary *dic = [_array objectAtIndex:indexPath.row];

    [cell setCustomDic:dic];

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *strAtt = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};

    NSDictionary *dic = [_array objectAtIndex:indexPath.row];

    NSString *value = [dic objectForKey:@"name"];

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    CGRect strRect = [value boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:strAtt context:nil];

    return strRect.size.height;

}

第四步:

AppDelegate.m


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _window.backgroundColor = [UIColor whiteColor];

    [_window makeKeyAndVisible];

    [_window release];

    

    mainViewController *main = [[mainViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];

    UITabBarController *tab = [[UITabBarController alloc] init];

    NSMutableArray *array = [NSMutableArray arrayWithObject:nav];

    tab.viewControllers = array;

    [_window setRootViewController:tab];

    [main release];

    [nav release];

    [tab release];













本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 根据文字内容设置cell 的高度 的相关文章

随机推荐

  • VS2008用devenv.com命令行工具自动编译工程

    转自 xff1a http www cr173 com html 18500 1 html 在vs2008下面提供了devenv com命令行方式 我们可以从VS安装目录 MicrosoftVisual Studio 9 Common7 I
  • 使用ICMP协议检测网络状态

    ICMP xff08 Internet ControlMessages Protocol xff0c 网间控制报文协议 xff09 是TCP IP协议族的子协议 xff0c 是一种面向无连接的协议 xff0c 在IP和路由器之前传递控制消息
  • c++打印enum class

    span class token keyword enum span span class token keyword class span span class token class name A span span class tok
  • 使用strace查找Emacs启动阻塞的原因(exec-path-from-shell)

    原文地址 https www lujun9972 win blog 2019 09 26 使用strace查找emacs启动阻塞的原因 exec path from shell index html 之前就觉得我的Emacs启动好慢 xff
  • 为Linux安装虚拟PDF打印机

    原文地址 https lujun9972 github io blog 2020 04 11 为linux安装虚拟pdf打印机 index html 今天发现一个 CUPS PDF 项目 可以为 CUPS Common Unix Print
  • ubuntu系统启用shell远程登陆

    Ubuntu desktop系统安装后 xff0c 想使用shell远程登陆 xff0c 会提示 Connecting to 192 168 220 133 22 Could not connect to 39 192 168 220 13
  • 枚举类(ENUM)用法总结

    对于ENUM一直是比较陌生的 xff0c 在和某酷爱ENUM的大神合作时 xff0c 才慢慢接触到ENUM的用法 1 ENUM是什么 xff1f 首先ENUM是一个类 xff0c 不像String int之类的数据结构 xff0c 更类似于
  • Python循环结构练习2

    Problem A xff1a 循环结构 输出数列2 xff0c 5 xff0c 8 xff0c 11 xff0c 14 题目描述 输入正整数n xff08 n 100 xff09 xff0c 输出数列2 xff0c 5 xff0c 8 x
  • KVM网络模型之:PCI Passthrough

    目录 PCI Passthrough技术介绍和KVM中配置 案例 内核启用 重新启动虚拟机实例 PCI Passthrough技术介绍和KVM中配置 PCI Passthrough技术是虚拟化网卡的终极解决方案 xff0c 能够让虚拟机独占
  • 微信开放公众平台,扩展自定义类,定时提醒,定时发消息

    微信开放公众平台 xff0c 扩展自定义类 xff0c 定时提醒 xff0c 定时发消息 lt php class MyapiAction extends BaseAction public function index 微医疗 预约提醒
  • Ubuntu配置iptables规则

    Ubuntu配置防火墙 xff0c 并且开机iptables自启动规则 适用于CentOS 1 登录root账号 span class token comment 切换到root账号 span super 64 super span cla
  • Linux记录用户执行命令

    span class token shebang important bin bash span span class token comment By lumia98 64 vip qq com span span class token
  • Nginx规则配置实例

    配置某个ip或者页面禁止访问及跳转方法 server listen 80 server name www test com cn location proxy redirect off proxy set header host host
  • Debian 11.2安装ssh服务

    切换到root用户 更新软件源 span class token function apt get span update 安装ssh span class token function apt get span span class to
  • Python计算文件大小

    span class token comment usr bin env python span span class token comment coding utf 8 span span class token triple quot
  • Python获取文件内的下一行数据

    span class token comment usr bin env python span span class token comment Version 61 3 8 1 span span class token comment
  • iptables配置实例

    查看当前所有规则 iptables L n 查看所有规则 iptables nL line number 显示行 iptables nvL line number 显示行 清空所有配置 iptables F iptables X iptab
  • 利用Shell脚本校验数据一致性

    span class token shebang important bin bash span span class token comment span span class token comment 检测两台服务器指定目录下的文件一
  • Debian11系统Redis源码安装

    span class token shebang important bin bash span span class token comment Debian11 Redis6 2 6安装 span span class token co
  • iOS 根据文字内容设置cell 的高度

    今天学习一个简单的 根据内容的大小设置cell 的高度 第一步 建两个类 分别继承于UIViewController 和UITableViewCell 第二步 mainViewController h import lt UIKit UIK