Tableview中cell的自适应高度

StudentCell.m文件   //自适应行高既要里面的label适应 又要cell自适应,所以应该有两处改动
@implementation StudentCell
- (void)dealloc
{
    [_nameLabel release];
    [_sexLabel release];
    [_phoneLabel release];
    [_photoView release];
    [_hobbyLabel release];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.photoView = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 2, 50, 40)] autorelease];
        self.nameLabel = [[[ UILabel alloc] initWithFrame:CGRectMake(60, 2, 100, 30)] autorelease];
        self.sexLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 50, 100, 30)] autorelease];
        self.phoneLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 100, 200, 30)] autorelease];
        self.hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 140, 200, 100)];
        self.hobbyLabel.numberOfLines = 0;//想要自适应宽高就把行数设置为0
        self.hobbyLabel.font = [UIFont systemFontOfSize:16];这里是设置字体大小
        
        [self addSubview:self.nameLabel];
        [self addSubview:_sexLabel];//这里用下划线还是点都是一样的
        [self addSubview:_photoView];
        [self addSubview:_phoneLabel];
        [self addSubview:_hobbyLabel];
        // Initialization code
    }
    return self;
}
- (void)newFrame
{
    self.photoView.frame = CGRectMake(270, 2, 50, 40);
    self.nameLabel.frame = CGRectMake(215, 2, 100, 30);
    self.sexLabel.frame = CGRectMake(215, 50, 100, 30);
    self.phoneLabel.frame =CGRectMake(115, 100, 200, 30);
    self.hobbyLabel.frame =CGRectMake(115, 140, 200, 100);
}
- (void)awakeFromNib
{
    // Initialization code
}
- (void)setStudent:(Students *)student
{   //重写set
    self.nameLabel.text = student.name;
    self.sexLabel.text = student.sex;
//    if ([student.sex isEqualToString:@"女"]) {
//        [self newFrame];
//    }
    CGRect rect =[student.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.hobbyLabel.font,NSFontAttributeName, nil] context:nil];
    self.hobbyLabel.frame = CGRectMake(115, 140, 200,rect.size.height);//指定字号,指定宽度下面得到高度是多少
    self.phoneLabel.text = student.phone;
    self.hobbyLabel.text = student.hobby;
    self.photoView.image = [UIImage imageNamed:student.photo];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

RootTableViewController.m文件代码
<pre name="code" class="html">@implementation RootTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"plist"];//这是一个单例 获取文件的地址一般用这个
    
    NSArray *students = [[NSArray alloc] initWithContentsOfFile:filePath];//用实例变量数组接受一下文件中的数据
    _studentArray = [[NSMutableArray alloc] initWithCapacity:[students count]];
    for (int i = 0; i < [students count]; i++) {
        NSDictionary *dic = [students objectAtIndex:i];
        Students *stu = [[Students alloc] init];
        [stu setValuesForKeysWithDictionary:dic];//KVC赋值
        [_studentArray addObject:stu];
        [stu release];
        
    }
    [students release];
    
//    NSLog(@"%@",_studentArray);
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_studentArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //*****方案三 用自己继承与UITableViewCell的类 自己定义属性
    
    Students *stu = [_studentArray objectAtIndex:indexPath.row];
    if ([stu.sex isEqualToString:@"男"]) {
        static NSString *identifier = @"reuse";
        StudentCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell1==nil) {
            cell1 = [[StudentCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell1.student = _studentArray[indexPath.row];
        return cell1;
    }
    
    else
    
    {
    static NSString *cellIdentifier = @"reuse";
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        
        cell = [[SecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.student = _studentArray[indexPath.row];//最简洁一行代码
    
    
//    Students *stu = [_studentArray objectAtIndex:indexPath.row];
//    [cell setStudent:stu];
    
    
//    cell.nameLabel.text = stu.name;
//    cell.sexLabel.text = stu.sex;
    if ([stu.sex isEqualToString:@"女"]) {
          [cell newFrame];
    }
//    cell.phoneLabel.text = stu.phone;
//    cell.photoView.image = [UIImage imageNamed:stu.photo];
//    cell.hobbyLabel.text = stu.hobby;
    return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//设置行高 这个很重要
    
    Students *stu = [_studentArray objectAtIndex:indexPath.row];
    if ([stu.sex isEqualToString:@"男"]) {
        
        
        
        CGRect rect =[stu.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil] context:nil];
        //这里可以把这行代码放在外面,我的这里返回值没有改变,其实可以改变一下男女不同高度返回值
        
        return 140+rect.size.height;
    }
         else
         {
             CGRect rect =[stu.hobby boundingRectWithSize:CGSizeMake(200, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil] context:nil];

           return 140+rect.size.height;
         }
    
    
    return 0;
}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //*****方案二 ,把建立label写进if语句中,这样label就一起重用了 不会出现重叠现象
//    
//    static NSString *cellIdentifier = @"reuse";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//    if (cell == nil) {
//        
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//        
//    UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,0, 150, 40)];
//    sexLabel.tag = 1000;
//    [cell.contentView addSubview:sexLabel];//自己创建一个sexlabel加到cell的contentview上面
//    [sexLabel release];
//        
//    }
//    
//    
//    UILabel *sexLabel = (UILabel *)[cell.contentView viewWithTag:1000];//因为if中sexLabe是个局部变量,所以需要出来接收一下在使用.接收的时候使用tag值.
//    NSDictionary *stu = [_studentArray objectAtIndex:indexPath.row];
//    cell.textLabel.text = [stu objectForKey:@"name"];
//    sexLabel.text = [stu objectForKey:@"sex"];
//    
//    
//    return cell;
//
//}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //******方案一 创建label在if语句外面 需要在创建一个label之前先消除同样tag值得label
//    static NSString *cellIdentifier = @"reuse";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//    }
//    [[cell.contentView viewWithTag:1000] removeFromSuperview];//每次先remove掉tag值为1000的再添加,否则会一层层重叠label
//    UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,0, 150, 40)];
//    sexLabel.tag = 1000;
//    [cell.contentView addSubview:sexLabel];//自己创建一个sexlabel加到cell的contentview上面
//    [sexLabel release];
//    
//    
//    
//    
//    NSDictionary *stu = [_studentArray objectAtIndex:indexPath.row];
//    cell.textLabel.text = [stu objectForKey:@"name"];
//    sexLabel.text = [stu objectForKey:@"phone"];
//    
//    // Configure the cell...
//    
//    return cell;
//}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像的目标属于哪个类别。 定位问题:确定目标在图像的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值