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];
    }


  • 相关阅读:
    vue create is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You
    Vue2.x是怎么收集依赖的
    只绑定一次事件的简单方法
    Proxy是怎么做数据劫持的
    使用babel进行打包
    使用npm link进行模块调试
    Webpack 热加载插件的实现原理
    Vue 服务端渲染的数据流
    Vue的生命周期钩子
    Linux定时任务
  • 原文地址:https://www.cnblogs.com/fortunely/p/4472174.html
Copyright © 2011-2022 走看看