zoukankan      html  css  js  c++  java
  • IOS中TableView的使用(1) 创建一个简单的tableView

    创建一个简单的tableView:

     1 #import <UIKit/UIKit.h>
     2  
     3 /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITableViewDelegate */
     4 
     5 @interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>
     6 {
     7     UITableView *_tableView;
     8  }
     9 @property (strong,nonatomic)UITableView *tableView;
    10 
    11 @end
    12 
    13 
    14 
    15 #import "ViewController.h"
    16 
    17 @interfaceViewController ()
    18 
    19 @end
    20 
    21 
    22 
    23 @implementation ViewController
    24 
    25 - (void)viewDidLoad
    26 {
    27     [superviewDidLoad];
    28 tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height) style:UITableViewStylePlain];
    29 
    30 
    31 //    UITableViewStylePlain,   普通             
    32 //    UITableViewStyleGrouped 分组
    33   
    34     tableview.delegate = self; //设置代理/
    35     tableview.dataSource=self; 
    36     [self.viewaddSubview:tableview];
    37     
    38 }
    39 
    40 /* 这个方法是显示tableView中有多少组,不实现该方法默认为1组*/
    41 -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
    42 {
    43     return 1;
    44 }
    45 /* 该方法表示每组有多少cell*/
    46 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    47 {
    48     return 10;
    49 }
    50 
    51 
    52 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    53 {
    54     
    55     //定义个静态字符串为了防止与其他类的tableivew重复
    56     static NSString *CellIdentifier =@"Cell";  
    57     //定义cell的复用性当处理大量数据时减少内存开销
    58     UITableViewCell *cell = [tableview  dequeueReusableCellWithIdentifier:CellIdentifier];
    59     
    60     if (cell ==nil)
    61     {  
    62         cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier];
    63      }
    64     
    65 return cell;
    66 }
    67 
    68 @end

    运行结果:

    如果你错过了一天,那么你就真的错过了一天……ues.hk
  • 相关阅读:
    git分支操作
    redis 和 memcached 有什么区别?redis 的线程模型是什么?为什么 redis 单线程却能支撑高并发?
    缓存如果使用不当会造成什么后果?
    在项目中缓存是如何使用的?
    excel poi3.17导出导入
    Mongodb: Sort operation used more than the maximum 33554432 bytes of RAM
    VMware12上安装CentOS7
    校验文件是否是Excel文件
    读后感——《构建之法》第1.2.3章
    操作系统——实验一
  • 原文地址:https://www.cnblogs.com/myios/p/3669174.html
Copyright © 2011-2022 走看看