• UITableViewController


    与UIViewController有区别,就是UITableViewController自身的带的是tableView。

    AppDelegate.m

    #import "AppDelegate.h"

    #import "RootTableViewController.h"

    @interface AppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Override point for customization after application launch.

        self.window.backgroundColor = [UIColor whiteColor];

       

        RootTableViewController *rootTVC = [[RootTableViewController alloc]initWithStyle:(UITableViewStyleGrouped)];

        UINavigationController *ncRoot = [[UINavigationController alloc]initWithRootViewController:rootTVC];

        self.window.rootViewController = ncRoot;

       

        [self.window makeKeyAndVisible];

       

        [ncRoot release];

        ncRoot = nil;

        [rootTVC release];

        rootTVC = nil;

       

        return YES;

    }

    解释:

    1、与原来的UIViewController一样,创建出来的RootTableViewController的对象rootTVC,style可以用plain和grouped两种。

    2、UINavigationController创建出来是用rootTVC作为参数创建的。

    RootTableViewController.m

    #import "AppDelegate.h"

    #import "RootTableViewController.h"

    @interface AppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Override point for customization after application launch.

        self.window.backgroundColor = [UIColor whiteColor];

       

        RootTableViewController *rootTVC = [[RootTableViewController alloc]initWithStyle:(UITableViewStyleGrouped)];

        UINavigationController *ncRoot = [[UINavigationController alloc]initWithRootViewController:rootTVC];

        self.window.rootViewController = ncRoot;

       

        [self.window makeKeyAndVisible];

       

        [ncRoot release];

        ncRoot = nil;

        [rootTVC release];

        rootTVC = nil;

       

        return YES;

    }

    解释:

    1、与原来的UIViewController一样,创建出来的RootTableViewController的对象rootTVC,style可以用plain和grouped两种。

    2、UINavigationController创建出来是用rootTVC作为参数创建的。

    RootTableViewController.m

     

    #import "RootTableViewController.h"

    #define kCELL_ID @"cell_1"

    @interface RootTableViewController ()

     

    @end

     

    @implementation RootTableViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // 注册,注册要用的哪个cell

       

        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCELL_ID];

    }

    #pragma mark - Table view data source

     

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

     

      

        return 2;

    }

     

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     

     

        return 10;

    }

     

     

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

       

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCELL_ID forIndexPath:indexPath];

     

     cell.textLabel.text = [NSString stringWithFormat:@"section:%ld,row:%ld",indexPath.section,indexPath.row];

       

        return cell;

    }

     

     

    /*

    // Override to support conditional editing of the table view.

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

        // Return NO if you do not want the specified item to be editable.

        return YES;

    }

    */

    /*

    // Override to support editing the table view.

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            // Delete the row from the data source

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        } else if (editingStyle == UITableViewCellEditingStyleInsert) {

            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

        }  

    }

    */

    /*

    // Override to support rearranging the table view.

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    }

    */

     

    /*

    // Override to support conditional rearranging of the table view.

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

     

      // Return NO if you do not want the item to be re-orderable.

        return YES;

    }

    */

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

    @end

    解释:

    1、UITableViewController已经帮我们封装了我们实现代理那些方法,就是上面那些注释起来的方法。

    2、[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCELL_ID],是对我们将要用到的cell进行注册。第一个参数,是UITableViewCell类,第二个参数,是我们设的宏定义,用来作为cell的重用字符。

  • 相关阅读:
    echarts饼状图位置设置
    去除echarts饼状图的引导线
    VS2008里的代码如何格式化
    使用ADO如何获得SQLSERVER 2K的数据库名的列表
    CStdioFile.WriteString无法向文件写入中文
    使用ODBC 数据库 ,运行程序时 出现 “遇到不适当的参数”
    CListCtrl消息及解释
    VC的CListCtrl控件
    找不到Microsoft Access Driver(*.mdb)ODBC驱动程序的安装例程。请重新安装驱动
    WebBrowser 控件-说明
  • 原文地址:https://www.cnblogs.com/Coder-GT/p/4886169.html
Copyright © 2020-2023  润新知