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

运行结果:

IOS中TableView的使用(1) -创建一个简单的tableView