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

    http://my.oschina.net/joanfen/blog/137601

    效果如下图:可触发按钮事件

    1、创建一个Empty Application

    2、新建一个TableViewController,命名为MyTable

    2.1在AppDelegate.h中添加@class 和property

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #import <UIKit/UIKit.h>
     
    @class MyTable;
     
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
     
    @property (strong, nonatomic) UIWindow *window;
     
    //声明
    @property (strong, nonatomic) MyTable *MyTableView;
     
    @end

    2.2在AppDelegate.m的 didFinishLaunchingWithOptions方法中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
         
        self.window.backgroundColor = [UIColor whiteColor];
         
        _MyTableView = [[MyTable alloc] initWithNibName:@"MyTable" bundle:nil];
         
        //创建一个navigationController,也可不创建,直接将window的rootViewController设定为MyTableView,此处创建是为了让程序有NavigationController的属性,方便Push视图
        UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:self.MyTableView];
         
        self.window.rootViewController = nv;
         
        [self.window makeKeyAndVisible];
        return YES;
    }

    2.3、在MyTable的viewWillAppear方法中

    1
    2
    3
    4
    5
    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        self.title = @"自定义Table";
    }

    3、新建一个UITableViewCell类,命名为MyCell

    4、新建一个View,命名与上面的相同(也可不同,只是命名相同更加方便)

    4.1、将此View中的View删除,拖一个TableViewCell进来,将此tableViewCell的Custom Class改成3中新建的类MyCell

    4.2.1、在cell中添加一个Label,创建映射

    4.2.2、在Cell中添加一个TextField,创建映射

    4.3.3、在Cell中添加一个Switch,创建映射

    4.3.4、在Cell中添加一个segment,创建映射

    5、在MyTable.m中,补充tableViewDataSource方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
     
        // Return the number of sections.
        return 1;
    }
     
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
     
        // Return the number of rows in the section.
        return 4;
    }

    补充CellForRow方法

    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
    38
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"MyCell";
        //自定义cell
        MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell ==nil) {
            //加载MyCell.xib文件,此处loadNibNamed后面的参数CellIdentifier必须与MyCell.xib文件名相同,否则会无法加载,报错崩溃
            NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
            cell = (MyCell *)[nibArray objectAtIndex:0];
             
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         
        NSArray *array = [NSArray arrayWithObjects:@"姓名",@"性别",@"学历",@"保险", nil];
         
        cell.title.text = [array objectAtIndex:indexPath.row];
         
        //根据行数来确定每行内容
        if (indexPath.row == 0||indexPath.row==2) {
            cell.textField.hidden = NO;
            cell.swich.hidden = YES;
            cell.segment.hidden = YES;
        }
        else if(indexPath.row == 1){
            cell.textField.hidden = YES;
            cell.segment.hidden = NO;
            cell.swich.hidden = YES;
        }
        else if(indexPath.row == 3)
        {
            cell.textField.hidden = YES;
            cell.swich.hidden = NO;
            cell.segment.hidden = YES;
        }
        //设置TextField代理
        cell.textField.delegate = self;
        return cell;
    }

     

  • 相关阅读:
    convert.c:7:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by
    80 多个 Linux 系统管理员必备的监控工具
    控件风格19种,必须倒背如流——其实就是控件所拥有的能力,即有条件使用VCL框架所提供的(功能)代码
    控件状态11种,必须倒背如流——记录控件当前的状态,防止误判(一般使用完以后就把状态改回去)
    FindChildControl与FindComponent
    保存网页为图片——滚动截取IE(WebBrowse)
    HDOJ 1755
    IP编辑控件(因为封装的是系统自带控件,所以也使用了CreateSubClass,不过为啥要封装CN_COMMAND和CN_NOTIFY不是很明白)
    QT之深入理解QThread
    Qt读取ANSI格式文件——利用QTextCodec将其他编码格式的QByteArray转换为Unicode格式,或者从文件中读出后直接做转换
  • 原文地址:https://www.cnblogs.com/ruiati/p/4332480.html
Copyright © 2011-2022 走看看