zoukankan      html  css  js  c++  java
  • 自定义UITableViewCell

    from:http://blog.csdn.net/u012350430/article/details/51181728

    自定义UITableViewCell

    创建一个TableViewController类继承于UITableViewController,创建一个TableViewCell类继承于UITableViewCell。

    AppDelegate.m编写代码如下

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        TableViewController *view = [[TableViewController alloc] init];
        self.window.rootViewController = view;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    自定义Cell,在TableViewCell.m中编写代码如下

    //cell自定义用的是-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法
    -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
        {
            //这里顺便介绍小UIButton的创建
            //设置button的类型是UIButtonTypeRoundedRect
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
            //设置button的frame
            button.frame = CGRectMake(20, 20, 50, 50);
    
            //button正常状态title设置为Yes,被选择状态title设置为No
            [button setTitle:@"Yes" forState:UIControlStateNormal];
            [button setTitle:@"No" forState:UIControlStateSelected];
    
            //设置button响应点击事件的方法是buttonPressed:
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            //添加到cell
            [self addSubview:button];
    
            //创建imageView添加到cell中
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Totoro副本"]];
            imageView.frame = CGRectMake(150, 20, 150, 100);
            [self addSubview:imageView];
    
        }
        return self;
    }
    
    //buttonPressed:方法
    -(void)buttonPressed:(UIButton *)button
    {
        //实现按钮状态的切换
        button.selected = !button.selected;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    TableViewController.m中编写如下代码

    //用来指定表视图的分区个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        //分区设置为1
        return 1;
    }
    
    //用来指定特定分区有多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        //设置为20行
        return 20;
    }
    
    //配置特定行中的单元格
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *ID = @"cell";
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
    
            //单元格样式设置为UITableViewCellStyleDefault
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }    
        return cell;
    }
    
    //设置单元格的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
    {
        //这里设置成150
        return 150;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    运行代码,结果如下图所示 
    这里写图片描述

     
  • 相关阅读:
    关于AutoResetEvent和ManualResetEvent
    (转)使用 ODP.NET 和引用游标优化结果集
    胰腺
    SQL Cache Dependency
    败犬的远吠?
    吃亏和付出经常是必须的(转)
    AutoResetEvent 与 ManualResetEvent送花例子
    linux yum install
    SpringMVC+JPA+SpringData配置
    Spring AOP 实现原理
  • 原文地址:https://www.cnblogs.com/lucky-star-star/p/5721942.html
Copyright © 2011-2022 走看看