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

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

     
  • 相关阅读:
    poj 2763 Housewife Wind
    hdu 3966 Aragorn's Story
    poj 1655 Balancing Act 求树的重心
    有上下界的网络流问题
    URAL 1277 Cops and Thieves 最小割 无向图点带权点连通度
    ZOJ 2532 Internship 网络流求关键边
    ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
    SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流
    怎么样仿写已知网址的网页?
    5-10 公路村村通 (30分)
  • 原文地址:https://www.cnblogs.com/lucky-star-star/p/5721942.html
Copyright © 2011-2022 走看看