zoukankan      html  css  js  c++  java
  • UITableView

    转自:UITableView

    ios中UITableView,也就是表视图,这在ios系统中是非常常见的,开发这种表视图也是非常方便的,在工具栏中,直接将UITableView拖拉到xib中即可,如下:

    uitable1 UITableView

    UITableView

    在UIViewController之后添加<UITableViewDelegate, UITableViewDataSource>,UITableViewDelegate和 UITableViewDataSource在Objective-C中称之为协议,要实现协议中必要的方法(因为有可选的方法)。为什么要使用这两个协议呢?因为我们要将数据填充到UITableView中,这样子,那UITableViewDelegate和 UITableViewDataSource应该与数据填充有关了,其实看它的命名也可以看得出了。

    UITableViewDataSource是用来连接数据和表视图的,要实现两个方法,一个是tableView:cellForRowAtIndexPath,另一个是tableView:numberOfRowsInSection,实现这两个方法,你就告诉了表视图显示多少行数据和每一行中的数据。

    UITableViewDelegate是负责处理UITableView的表现,该协议中的可选方法让你管理表行的高度,配置节点头部和底部,对表单元重新排序等。

    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
    #import "SimpleTableViewController.h"
     
    @implementation SimpleTableViewController
     
    NSArray *tableData;
     
    - (void)didReceiveMemoryWarning
    {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
     
    // Release any cached data, images, etc that aren't in use.
    }
     
    #pragma mark - View lifecycle
     
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    }
    */
     
    - (void)viewDidUnload
    {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }
     
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
     
    @end

    viewDidLoad是在控制器的视图装载到内存中完成之后,调用该方法,添加以下代码实例化tableData数组:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        tableData=[NSArray arrayWithObjects:@"Egg Benedict",
                   @"Mushroom Risotto",@"Full Breakfast",
                   @"Hamburger",@"Ham and Egg Sandwich",
                   @"Creme Brelee",@"White Chocolate",
                   @"Starbucks Coffee",@"Vegetable Curry",
                   @"Instant Noodle with Egg",nil];
    }

    实现UITableViewDataSource协议中的两个方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    //count
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [tableData count];
    }
     
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier=@"SimpleTableItem";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if(cell==nil){
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
        cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
        return cell;
    }

    count方法返回tableData数组中元素个数,每行数据显示的时候,会调用cellForeRowAtIndexPath 方法。dequeueReusableCellWithIdentifier: 方法返回的是一个可重用的表规图单元格对象。因为如果表非常大,为每一行都创建一个单独的 UITableViewCell对象会产生严重的性能问题,会占用大量的内存。 此外,由于表视图在某一个时刻会显示固定数量的行,因此重用已经滚动到屏幕外面的那些单元格将非常有意义。 这正是dequeueReusableCellWithIdentifier: 方法将要完成的事情。比如,如果表视图显示了 10 行,那么会创建 10 个 UITableViewCell 对象 — 当用户滚动表视图时,总是会重用返 10 个 UITableViewCell 对象。

    SimpleTableViewController.xib 文件,点击并按住 Control 键,选择表视图,并拖拉到 Files Owner图上,释放按钮,弹出 dataSource 和 delegate 窗口。选择 dataSource,在表视图和它的数据源之间建立连接。重复上述操作,在委托(delegate)上也建立连接。

    运行如下:

    iphone UITableView
  • 相关阅读:
    RabbitMQ In JAVA 介绍及使用
    JSON Web Token 入门教程
    char* 与 char[] 的区别
    C++ namespace的用法
    启动其他APK的Activity方法 (转至http://www.cnblogs.com/lijunamneg/archive/2013/02/26/2934060.html)
    Android多任务切换与Activity启动模式SingleTask之间关系的分析
    对SingleTask和TaskAffinity的理解(转至 http://www.2cto.com/kf/201311/254450.html)
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    大牛的博客
    Androidpn 简单实现及分析
  • 原文地址:https://www.cnblogs.com/hxxy2003/p/3023673.html
Copyright © 2011-2022 走看看