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

时间:2023-03-08 22:20:24
IOS中TableView的使用(1) -创建一个简单的tableView

创建一个简单的tableView:

 #import <UIKit/UIKit.h>

 /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITableViewDelegate */

 @interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@property (strong,nonatomic)UITableView *tableView; @end #import "ViewController.h" @interfaceViewController () @end @implementation ViewController - (void)viewDidLoad
{
[superviewDidLoad];
tableview = [[UITableViewalloc]initWithFrame:CGRectMake(, ,self.view.bounds.size.width,self.view.bounds.size.height) style:UITableViewStylePlain]; // UITableViewStylePlain, 普通
// UITableViewStyleGrouped 分组 tableview.delegate = self; //设置代理/
tableview.dataSource=self;
[self.viewaddSubview:tableview]; } /* 这个方法是显示tableView中有多少组,不实现该方法默认为1组*/
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return ;
}
/* 该方法表示每组有多少cell*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ //定义个静态字符串为了防止与其他类的tableivew重复
static NSString *CellIdentifier =@"Cell";
//定义cell的复用性当处理大量数据时减少内存开销
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell ==nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
} return cell;
} @end

运行结果:

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