一、简介 UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。大家先看一下两者的应用: 二、代码示例 1.TableViewController.xib 配置TableView,选择View下面的TableView鼠标右键,看到dataSource和delegata,按着confrol分别拖动到dataSource到File’s Owner进行关联,关联后就如上图所示。 2.TableViewController.h
#import <UIKit/UIKit.h> @interface TableViewController : UIViewController<UITabBarDelegate,UITableViewDataSource> @property(nonatomic,retain)NSArray* list; @end3.TableViewController.m
#import "TableViewController.h" @interface TableViewController () { __weak IBOutlet UIButton *BackButton; } @end @implementation TableViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. NSArray *array=[[NSArray alloc]initWithObjects:@"c",@"java",@"c++",@"oc",@"Python",@"go",@"c#",@"js",@"javaweb",@"asp.net", nil]; self.list = array; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(IBAction)back:(id)sender{ [self dismissViewControllerAnimated:YES completion:nil]; } -(void)viewDidUnload { [super viewDidLoad]; self.list=nil; } //返回总行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.list count]; } //为每一行赋值 static NSString *SimpleTableIdentifier=@"SimpleTableIdentifier"; -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; if(cell==nil){//如果行元素为空的话 则新建一行 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]; } //取得当前行 NSUInteger row=[indexPath row]; //设置每一行要显示的值 cell.textLabel.text=[_list objectAtIndex:row]; return cell; } //设置点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //该方法响应列表中行的点击事件 NSString *heroSelected=[_list objectAtIndex:indexPath.row]; //indexPath.row得到选中的行号,提取出在数组中的内容。 UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"语言" message:heroSelected delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [myAlertView show]; //点击后弹出该对话框。 } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end然后运行。运行结果如第一张图。