zoukankan      html  css  js  c++  java
  • 关于iOS中TableVIew(列表)的自定义创建和自定义的Cell

    最近研究了一些HTML5的基础,一些C++的基础,有些冷落了我的iOS技术,以至于最近对于iOS有种没有信心的感觉,所以今天开始回归我的iOS核心技术,眼前表现为回顾iOS技术,以博客的形式,写总结,好吧,废话不多说

    纯代码形式创建:1.创建tableView

              2.定义一个自定义Cell

            3.设置代理

            4.代理方法的我实现

    tableView的创建主要有以下步骤:

    1.创建tableView    

    - (void)createTableView
    {

    //初始化tableView并定义位置,大小。
        UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];

    //设置table代理的数据源和代理为自己
        tableView.delegate =self;
        tableView.dataSource = self;

    //为table 注册自定义的Cell的类。注册方法如下
        [tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];

    //加入视图。
        [self.view addSubview:tableView];
        
    }

    2.自定义Cell


    @interface MyCell : UITableViewCell
    @property(nonatomic,strong) UILabel * label;
    @property(nonatomic,strong) UIImageView * image;

    @end

    @implementation MyCell

    - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        
        self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.label  =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
            
            self.image  = [[UIImageView alloc]initWithFrame:CGRectMake(20, 30, 50, 50)];
            
            [self.contentView addSubview:self.image];
            [self.contentView addSubview:self.label];
            
        }
        return self;
    }

    @end

    3.设置代理

    @interface MyTableViewController ()<UITableViewDelegate,UITableViewDataSource>

    4.代理方法的实现:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 15;
    }


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

    //为Cell设置重用的ID
        MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    //如果cell没有才创建
        if (cell==nil) {
            cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        
        return cell;
        
    }

  • 相关阅读:
    观察 HTML中id和name 的差异,被微软忽悠过的同学自动举手
    ScottGu中文博客: 新捆绑和缩小支持(ASP.NET 4.5系列)
    在证书存储区中找不到清单签名证书
    HTML中的转义字符
    SQL 查询本周/本月/本季度/本年的数据
    sql STUFF用法
    jQuery UI Autocomplete 体验
    你必须知道的ADO.NET(二)了解.NET数据提供程序
    ASP.NET MVC的全球化方案
    MongoDB是什么?
  • 原文地址:https://www.cnblogs.com/YaoWang/p/4786280.html
Copyright © 2011-2022 走看看