iOS UITableView cell自适应内容高度

2023-05-16

定义UITableView 并且遵守两个协议

    firstTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];

    firstTableView.separatorStyle = NO;

    firstTableView.delegate = self;

    firstTableView.dataSource = self;

    [self.view addSubview:firstTableView];


//返回每个cell的高度

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

{

    FirstFrame *firstFrame = [frameArr objectAtIndex:indexPath.row];

    return firstFrame.heightTableViewCellRow;


}


//cell的内容

/*

                FirstData *firstData = [FirstData arrayWithDic:dic];//网络加载json解析为对象

                FirstFrame *firstFrame = [[FirstFrame alloc]init];//计算cell高度的累

                firstFrame.firstData = firstData;

                

                [firstDataArr addObject:firstData];//数据类数组

                [frameArr addObject:firstFrame];//cell高度类数组

*/

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

{

    FirstTableViewCell *cell = [FirstTableViewCell theCorpusWithTableView:tableView];//自定义cell

    cell.firstFrame = [frameArr objectAtIndex:indexPath.row];

    cell.firstData = [firstDataArr objectAtIndex:indexPath.row];

    return cell;

}


#import "FirstTableViewCell.h"

#import "FirstFrame.h"

#import "FirstData.h"

@implementation FirstTableViewCell

+(instancetype)theCorpusWithTableView:(UITableView *)tableView

{

    static NSString *cellID = @"cellid";

    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {

        cell = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;

}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        _lineView = [[UIView alloc]init];

        [self.contentView addSubview:_lineView];

        

        _timeBackView = [[UIView alloc]initWithFrame:CGRectMake(12, 15, 120, 17)];

        [self.contentView addSubview:_timeBackView];

  

        _titleLabel = [[UILabel alloc]init];

        [self.contentView addSubview:_titleLabel];

       

        

        _faceIageView = [[UIImageView alloc]init];

       [self.contentView addSubview:_faceIageView];

       

    

    }

    return self;

}

/*。 重写set方法

    cell.firstFrame = [frameArr objectAtIndex:indexPath.row];

    cell.firstData = [firstDataArr objectAtIndex:indexPath.row];

*/

-(void)setFirstData:(FirstData *)firstData

{

    _firstData = firstData;

    _timeLabel.text = _firstData.createtime;

    _titleLabel.text = _firstData.title;


}

-(void)setFirstFrame:(FirstFrame *)firstFrame

{

    _firstFrame = firstFrame;

    //将计算的控件frame赋值

    _lineView.frame = _firstFrame.lineViewFrame;

    _titleLabel.frame = _firstFrame.titleLabelFrame;

    _faceIageView.frame = _firstFrame.faceIageViewFrame;

}


#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@class FirstData;

@interface FirstFrame : NSObject

@property (nonatomic,strong) FirstData *firstData;


@property (nonatomic,assign,readonly) CGRect lineViewFrame;

@property (nonatomic,assign,readonly) CGRect titleLabelFrame;

@property (nonatomic,assign,readonly) CGRect faceIageViewFrame;


@property (nonatomic,assign) CGFloat heightTableViewCellRow;//cell高度




@end


#import "FirstFrame.h"

#import "FirstData.h"

@implementation FirstFrame

-(void)setFirstData:(FirstData *)firstData//重写set方法

{

    _firstData = firstData;

        CGRect rectname = [firstData.title boundingRectWithSize:CGSizeMake(kScreenWidth-46, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];

        _titleLabelFrame = CGRectMake(34, 47-32, kScreenWidth-46, rectname.size.height);

        _lineViewFrame = CGRectMake(16, 0, 1, 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32);

        _faceIageViewFrame = CGRectMake(34, 59 +rectname.size.height-32 , kScreenWidth-46, (kScreenWidth-46)*32/66.0);


        //cell高度

        _heightTableViewCellRow = 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32;


    

}

@end




//不明白的欢迎私聊,指正














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

iOS UITableView cell自适应内容高度 的相关文章

  • 在 Swift 中以编程方式为 iOS 制作带有名字首字母的图像,例如 Gmail

    我需要在 UITableView 中显示与其姓名相对应的每个用户的个人资料图片 在下载图像之前 我需要显示一张带有他名字的第一个字母的图像 就像在 GMail 应用程序中一样 如何在 Swift for iOS 中以编程方式执行此操作 不需
  • cameraOverlayView 防止使用 allowedEditing 进行编辑

    在我的应用程序中 使用以下行在拍摄照片后对其进行编辑 移动和缩放 效果很好 imagePicker setAllowsEditing YES 但如果我还使用cameraOverlayView 则编辑模式将不再起作用 屏幕出现 但平移和捏合手
  • 当 ViewController 从 UIStoryboard 实例化时,isMemberOfClass 返回 no

    我有一个 OCUnit 测试类 PatientTestViewControllerTests 下面是界面 interface PatientTestViewControllerTests SenTestCase property nonat
  • 在故事板中将 UITableView 的 rowHeight 设置为 UITableViewAutomaticDimension ?

    在 Xcode 6 中创建 iOS 8 应用程序时 如何设置 UITableViewrowHeight to UITableViewAutomaticDimension In WWDC 2014 第 226 场会议 表和集合视图中的新增功能
  • 如何在 Firebase Analytics 事件中报告参数

    我用过Fabric with iOS在此之前 在同一分析事件中报告自定义参数非常容易 如下所示 Answers logCustomEvent withName saved border customAttributes image inde
  • Socket.io 400(错误请求)

    我的服务器上有这段代码 var express require express var routes require routes var user require routes user var http require http var
  • 在 hooks 文件夹中使用节点脚本时,cordova 插件安装问题

    Cordova 3 4 hooks 未在 iOS 中正确安装提到的插件 我正在添加install plugins js进入文件夹project project root hooks after platform add其中有以下代码 usr
  • 使 iOS 应用程序与 iPhone 6 和 iPhone 6 尺寸兼容 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我创建了一个应用程序 其中使用 xib 进行布局 目前我使用两种不同的 xib 一种用于iPhone4 320 480 一种用于iPh
  • HTML 分页

    有没有html分页的开源项目 我正在为 iPhone 开发一个应用程序 我想在 UIWebView 上显示 HTML 文件 并且不希望用户向下滚动以查看屏幕上未显示的剩余内容 我想在第二个 UIWebView 上显示剩余的内容 我怎样才能做
  • 我们能否检测用户是否通过主页按钮或锁定按钮离开而没有监听 darwin 通知?

    我最近向应用程序商店提交了一个新的二进制文件并将其发送以供审核 但它立即被拒绝并显示以下消息 不支持的操作 不允许应用程序监听设备锁定通知 经过一番挖掘后 我发现我们无法使用 com apple springboard lockstate
  • 如何向 UIView 添加大小调整手柄?

    我试图根据用户请求在运行时动态创建视图 UIImageView 和 UITextView 然后允许用户移动它们并调整它们的大小 除了调整大小之外 我的一切都工作得很好 我尝试使用捏合手势识别器 但发现它对于我想要的东西来说太笨拙了 因此 我
  • 如何右对齐 UILabel?

    Remark 实施 myLabel textAlignment right does not解决了我的问题 这不是我所要求的 我想要实现的是让标签对齐右对齐 为了更清楚地说明 这就是如何left对齐外观 就是这样justify对齐外观 if
  • 通过 renderInContext 定位要绘制的视图:

    我想画一个UIView在我目前的CGGraphicsContext 我画的是UIView via renderInContext 但它的位置不正确 始终位于左上角 我拥有所有的价值观UIView可用于绘制UIView CGRect fram
  • 查找已用应用程序名称的捆绑包/开发人员

    我正在尝试将应用程序上传到应用程序商店并收到以下错误 很容易理解 The App Name you have entered has already been used 该应用程序不在 iTunes 上 有什么方法可以找出谁拥有该应用程序或
  • 以弯曲格式显示文本

    我正在寻找以曲线格式绘制一些文本 我使用哪个控件并不重要 UITextField UILabel or UITextView 我只想显示如图所示的文本 仍在寻找解决方案 请帮忙 查看此链接 https nodeload github com
  • 沙盒尝试恢复消耗性 IAP

    我一直在尝试在 iOS 上测试一些消耗性 IAP 但遇到了一个奇怪的错误 弹出一条警报 其中包含以下文本 此应用内购买已被购买 它将恢复为 自由的 环境 沙盒 我已经检查过 并且确定我的 IAP 可以在 iTunesConnect 中使用
  • AWS S3 公共对象与私有对象?

    回到 S3 我的存储桶中有图像的 URL 我将在我的应用程序中呈现这些图像 但它们被设置为私有 当我尝试单击该链接时 它显示 访问被拒绝 当我将链接的设置更改为公共时 它会通过 但是我读到公共访问并不是最安全的事情 所以这本质上是一个由两部
  • ios - Gamekit 的 GKOctree 未找到元素

    我正在尝试使用GKOctree https developer apple com documentation gameplaykit gkoctree用于高效检索 3D 空间中的对象 然而 以下代码似乎没有按预期工作 import Gam
  • 如何将相机中的图像保存到 iPhone 图库中的特定文件夹?

    嘿 我是 iPhone 新手 最近我一直在尝试制作一个应用程序 基本上 我想要做的是 如果用户将从相机捕获任何图像 那么它应该保存在设备库中 我知道如何将照片保存在图库中 它对我有用 但我无法将所有捕获的图像保存到设备图库中的特定文件夹 例
  • 在 HTML5 iOS 7 / iOS 8 中显示十进制键盘

    经过几个小时的搜索后 我只是有一个简单的问题 是否有可能在网络浏览器输入字段中显示小数键盘 input type number 只显示数字 但我需要在左下角使用逗号或点 我尝试过任何事情 pattern step等等 但没有显示十进制键盘

随机推荐