• [10]UITableView表示图2 自适应高度


    本篇文章我们使用plist文件
    先引入一个plist文件的读取

    //设置一个字符串读取文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"plist"];
    //通过文件路径存入到数组
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    

    由于我们的plist文件是数据学生信息,我们可以先一个学生类数据,来跟plist数据里的字典类型数据相匹配,注意的是 我们建的Person数据类要跟字典中的key相匹配,还有我们要修改cell的样式,我们可以选择继承于UITabelViewCell,重写初始化方法来自定义布局,

    我们新建数据类如下Person,personCell1,由于我们只是用来建造数据类型,只需要建我们需要的属性,和处理内存释放就可以了.

    //.h文件
    @interface Person : NSObject
    
    @property (nonatomic,retain) NSString *icon;
    @property (nonatomic,retain) NSString *name;
    @property (nonatomic,retain) NSString *phoneNumber;
    @property (nonatomic,retain) NSString *sex;
    @property (nonatomic,retain) NSString *introduce;
    @end
    //.m文件
    @implementation Person
    
    -(void)dealloc
    {
        [_icon release];
        [_name release];
        [_phoneNumber release];
        [_introduce release];
        [_sex release];
        [super dealloc];
    
    }
    
    -(void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
    
    }
    //上下俩个 是取值 和 过来没有key的值值
    - (id)valueForUndefinedKey:(NSString *)key
    {
        return nil;
    }
    @end
    

    我们新建的cell类,继承于UITableViewCell

    #import <UIKit/UIKit.h>
    @class Person;
    
    @interface PersonCell1 : UITableViewCell
    
    @property (nonatomic,readonly,retain) UIImageView *headImageView;
    @property (nonatomic,retain,readonly) UILabel *nameLabel;
    @property (nonatomic,retain,readonly) UILabel *phoneLabel;
    @property (nonatomic,retain,readonly) UILabel *introduceLabel;
    
    
    @property (nonatomic,retain) Person *person;
    
    
    //实现自适应高度,我们自己添加一个求cell的高度的方法
    + (CGFloat)cellHieghtForPerson:(Person *)p;
    
    @end
    
    
    //.m文件中的
    #import "PersonCell1.h"
    #import "Person.h"
    //由于这个方法我们只写在了本类中,我们定义为+方法,切写延展中,主要是外面也用不到
    @interface PersonCell1 ()
    //创建一个方法用来获得文本框的高度用的
    + (CGFloat )heightForString:(NSString *)str;
    
    @end
    
    @implementation PersonCell1
    
    -(void)dealloc
    {
        [_nameLabel release];
        [_introduceLabel release];
        [_person release];
        [_phoneLabel release];
        [_headImageView release];
        [super dealloc];
    }
    //重写UITableViewCell的初始化方法,并在里面给我们的空间定好位置布局
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 120)];
            [self.contentView addSubview:_headImageView];
            
            _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 210, 20)];
            [self.contentView addSubview:_nameLabel];
            
            _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 35, 210, 20)];
            [self.contentView addSubview:_phoneLabel];
            
            _introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 210, 70)];
            _introduceLabel.font = [UIFont systemFontOfSize:18];
            _introduceLabel.numberOfLines = 0;
            [self.contentView addSubview:_introduceLabel];
            
        }
        
        return  self;
    }
    //重写person属性的set方法,懒加载的方式再里面进行数据的匹配,让我们控件要显示的文本和Person类的属性一一对应起来
    -(void)setPerson:(Person *)person
    {
        if (_person != person) {
            [_person release];
            
            _person = [person retain];
            _headImageView.image = [UIImage imageNamed:person.icon];
            _nameLabel.text = person.name;
            _phoneLabel.text = person.phoneNumber;
            _introduceLabel.text = person.introduce;
            _introduceLabel.frame = CGRectMake(100, 60, 210, [PersonCell1 heightForString:person.introduce]);
            
        }
    
    }
    
    //计算文本框的高度
    + (CGFloat )heightForString:(NSString *)str
    {
    
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
        
        CGRect rect = [str boundingRectWithSize:CGSizeMake(210, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return rect.size.height;
    }
    //计算cell的高度
    + (CGFloat)cellHieghtForPerson:(Person *)p
    {
        //自我介绍label的y值加上自我介绍label的height 加上 最下面那块空白区域
        CGFloat height = 60 + [PersonCell1 heightForString:p.introduce]+10;
        return height > 140 ? height:140;
    }
    //以下为cell自带的方法
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    @end
    

    现在要给我们需要的根视图控制器结合,RootViewController

    #import <UIKit/UIKit.h>
    //接受这俩个协议,只要是使用UITabelView基本上都会使用这俩
    @interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
    //可变数组存放我们plist文件中的数据 只不过是用来先把原数据转换后存入的
    @property (nonatomic,retain) NSMutableArray *persons;
    //tableView表示图 设置为属性,方便使用
    @property (nonatomic,retain) UITableView *tableView;
    
    @end
    
    
    
    //.m文件中
    
    #import "RootViewController.h"
    #import "Person.h"
    #import "PersonCell1.h"
    #import "PersonCell2.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    -(void)dealloc
    {
        [_persons release];
        [_tableView release];
        [super dealloc];
    }
    
    -(void)loadView
    {
        //初始化
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        //设置显示条的颜色
        _tableView.separatorColor = [UIColor redColor];
        //接收俩个代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        self.view = _tableView;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"plist"];
        
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        //persons可变数组初始化开辟空间,这部不可省略!!!
        _persons = [[NSMutableArray alloc] initWithCapacity:[array count]];
        //通过forin快速遍历,把plist的文件转化成我们想要的person类型
        for (NSDictionary *dic in array) {
            Person *person = [[Person alloc] init];
            //kvc的一种模式 直接把字典转换,但是 person的属性设置 和 key一定要匹配 如果我们写的key值没有全部引用我们要在后面加方法
            [person setValuesForKeysWithDictionary:dic];
            
            [_persons addObject:person];
            [person release];
        }
        [array release];
    }
    //控制器中原有的警告方法
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //返回行高的方法 也就是代理中的方法
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Person * p = _persons[indexPath.row];
        if ([p.sex isEqualToString:@"女"] ) {
            return [PersonCell2 cellHieghtForPerson:_persons[indexPath.row]];
        }
        else
        {
        return  [PersonCell1 cellHieghtForPerson:_persons[indexPath.row]];
        }
    }
    //处理数据的代理中必须执行的方法之一,返回多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_persons count];
    }
    //处理数据的代理中必须要执行的方法之一,给我们的cell设值的方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Person *p = _persons[indexPath.row];
        if ([p.sex isEqual:@"女"]) {
            
            static NSString *resueIdentifier = @"person2";
            PersonCell2 *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:resueIdentifier];
            
            if (cell2 == nil) {
                cell2 = [[[PersonCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resueIdentifier] autorelease];
            }
            cell2.person = _persons[indexPath.row];
            return cell2;
        }
        else
        {
            static NSString *resueIdentifier = @"person1";
            PersonCell1 *cell1 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:resueIdentifier];
            
            if (cell1 == nil) {
                cell1 = [[[PersonCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resueIdentifier] autorelease];
            }
            cell1.person = _persons[indexPath.row];
             return cell1;
        }
    }
    
    @end
    
    On the road。。。
  • 相关阅读:
    Flutter和iOS混编详解
    Flutter异步与线程详解
    Nacos源码系列—服务端那些事儿
    Nacos源码系列—关于服务注册的那些事
    Nacos在企业生产中如何使用集群环境?
    踩到一个关于分布式锁的非比寻常的BUG!
    分享一个我看源码时的小技巧。
    通俗易懂的ArcGis开发快速入门
    Linux命令篇 grep 命令
    Linux命令篇 sed 命令
  • 原文地址:https://www.cnblogs.com/ianhao/p/4471817.html
Copyright © 2020-2023  润新知