iOS开发—UITableview控件简单介绍一、基本介绍在众多移动应⽤用中,能看到各式各样的表格数据 。在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,⽽且性能极佳 。UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置。
方法/步骤
1#import @interface ViewController : UIViewController@end
2#import 'ViewController.h'@interface ViewController ()@property(weak,nonatomic) IBOutlet UITableView *tableview;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //设置数据源 UITableView *tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.dataSource =self; [self.view addSubview:tableview]; }#pragma mark - UITableViewDataSource//1.tableview共有多少组-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSLog(@'numberofsectionintableview'); return 2;}//2.section有几行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@'numberofrowinsection %d', section); if (0==section) { return 2; } else { return 3; }}//3.告知每行显示什么-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@'cellforowatindexpath %d %d',indexPath.section,indexPath.row); UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; if (0==indexPath.section) { if (0==indexPath.row) { cell.textLabel.text= @'奥迪'; } else if(1==indexPath.row) { cell.textLabel.text =@'宝马'; } } else if(1== indexPath.section){ if (0==indexPath.row) { cell.textLabel.text=@'本田'; } else if(1==indexPath.row) { cell.textLabel.text=@'丰田'; }else if (2==indexPath.row) { cell.textLabel.text=@'马自达'; } } return cell;}//section组头显示什么-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (0==section) { return @'德系汽车'; }else { return @'日韩汽车'; }}//section组尾显示什么-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ if (0==section) { return @'高端大气上档次'; } else { return @'还不错哦'; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end