zoukankan      html  css  js  c++  java
  • IOS> TableView 用法

    1.在视图上创建TableView( 拖控件),为ViewController创建UITableView属性(链接至TableView)和NSArray属性(存储数据)

    ViewController.h
    @property (strong, nonatomic) NSArray *list;
    @property (weak, nonatomic) IBOutlet UITableView *tableView;

    2.为UIViewController实现UITableViewDelegate,UITableViewDataSource两个协议

    ViewController.h
    @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
    @property (strong, nonatomic) NSArray *list;
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    @end

    3.同步属性

    ViewController.c
    @synthesize list = _list;


    4.设置tableview数据源的数据,显示代理 以及显示区域等.

    ViewController.c
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
                          @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝", @"日本" ,nil];
        self.list = array;
        [self.tableView setFrame:CGRectMake(0, 0, 320, 420)];
        [self.tableView setDataSource:self];
        [self.tableView setDelegate:self];
        
    }


    5.绘制表格单元

    ViewController.c
    //Draw tableview cell
    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
        
        if(cell == nil){
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:TableSampleIdentifier];
        }
        
        NSUInteger row = [indexPath row];
        
        cell.textLabel.text=[self.list objectAtIndex:row];
        
        return cell;
    }
    
    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [self.list count];
    }


  • 相关阅读:
    ubuntu安装
    k8s 安装
    Blazor 路由
    ISO 8601
    Centos 8使用devstack快速安装openstack最新版
    使用devstack 一键安装 openstack详细过程和遇到的坑
    蓝瑟66000公里保养
    释放rsyslog占用的Linux内存
    Netty4.1 Http开发入门(一)服务端
    网络传输中的帧和payload
  • 原文地址:https://www.cnblogs.com/fortunely/p/4472174.html
Copyright © 2011-2022 走看看