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.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    jquery封装常用方法
    附加到iis进程调试时找不到w3wp.exe
    mongodb与sql语句对照表
    leetcode第一刷_Convert Sorted Array to Binary Search Tree
    swift 拼图小游戏
    散列技术之链地址法(基于无序链表)
    hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现
    xtrabackup 2.3.3编译安装
    Android 绘制圆形图片
    赫夫曼树的构建、编码、译码解析
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3697988.html
Copyright © 2011-2022 走看看