zoukankan      html  css  js  c++  java
  • ios-表视图-demo-自定义cell和心得

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
            static NSString *cellindentifier=@"cell";
        
        if (self.celltype==KTableViewCellContenview) {//第一种自定义cell
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
                UILabel*lable= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];//在这里创建性能更高,最少的创建实例滑动的时候
                lable.tag=101;
                [cell.contentView addSubview:lable];
            }
           UILabel * lable=(UILabel *) [cell.contentView viewWithTag:101];
            lable.text=_dataarray[indexPath.row];
            lable.font=[UIFont fontWithName:_dataarray[indexPath.row] size:16];
            return cell;
        
        }else if(self.celltype==KTableViewCellCustom){
            
            MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
            if (cell==nil) {
                cell=[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
                NSLog(@"排版前%d",indexPath.row);
            }
           
            return cell;
    
            
        }else if(self.celltype==KTableViewCellNib){//从nib来
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
            if (cell==nil) {
                NSArray*nibs=[[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:self options:nil];
                cell=[nibs objectAtIndex:0];
            }
            UILabel * lable=(UILabel *) [cell.contentView viewWithTag:102];
            lable.text=_dataarray[indexPath.row];
            lable.font=[UIFont fontWithName:_dataarray[indexPath.row] size:16];
            return cell;
            
        }else{
            
            return nil;
        }
        
        
        
    }
    首先是对我们为什么自定义系统的cell尺寸没用,因为ios会在我们创建了一屏幕的cell的时候会调用
    -(void)layoutSubviews{
        [super layoutSubviews];
        self.textlabel.frame=CGRectMake(0, 0, 300, 44);
    }
    对创建了的cell进行重新排版,当然这里的重新排版只会对contentview以外的进行排版,以后滑动屏幕的时候,可能是新建立的cell或者重用的都会进行排版,
    想要对contentview进行手动排版的话可以手动来覆写
    -(void)layoutSubviews{
        [super layoutSubviews];
        //在这里写,比如下面这个文件这样
    }
    //
    //  MyCell.m
    //  CustomCellDemo
    //
    //  Created by  liyang on 14-4-28.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "MyCell.h"
    
    @implementation MyCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            _label=[[UILabel alloc]initWithFrame:CGRectZero];
            _label.backgroundColor=[UIColor orangeColor];
            [self.contentView addSubview:_label];
        }
        return self;
    }
    -(void)layoutSubviews{
        [super layoutSubviews];
        _label.frame=CGRectMake(0, 0, 300, 44);
         NSLog(@"排版后%d");
    }
    @end
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    初识TPOT:一个基于Python的自动化机器学习开发工具
    为你的机器学习模型创建API服务
    iview 表格的多选框如何默认选中某项
    在uni-app中使用阿里巴巴图标库字体图标
    uni-app添加自定义底部导航栏,实现根据权限动态切换底部栏目的功能
    iview表格点击整行都可行切换前面的单选按钮或者多选按钮的状态
    uni-app引入vconsole调试移动端项目
    vue+view中的折叠面板点击表头阻止面板的收缩事件
    vue项目中判断文字内容超出n行显示详情按钮
    uni-app给顶部导航栏添加自定义字体图标
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3697988.html
Copyright © 2011-2022 走看看