1.创建一个简单的单元格
UITableView继承了UIScrollView,他具有UIScrollView的功能,UISCrollView中主要封装了UITableViewCell单元格控件,因此,UITableView默认可以对单元格进行滚动。在默认状态下,所有的UITableViewController实例被自动设定为UIScrollView委托
ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource>
@end
ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//作为UITableView显示数据的两个nsarray
@property(nonatomic,strong) NSArray* books;
@property(nonatomic,strong) NSArray* details;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建并初始化两个nsarray对象
_books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swifit讲义",@"疯狂ajax讲义"];
_details=@[@"android的内容还是很复杂的",@"相比安卓,IOS的内容要更容易上手一点",@"swift更新的有点太快了,什么时候能够稳定下来",@"ajax虽然技术上有些古老,但是前端正是火爆的时候"];
//为UITableView控件设置DataSource
self.tableView.dataSource=self;//为什么是self呢
//为UITableView控件设置页眉、页脚控件
self.tableView.tableHeaderView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableheader.png"]];
self.tableView.tableFooterView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tablefooter.png"]];
}
//该方法返回各表格行的控件
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//为表格行定义一个静态字符串作为标志符,是为了重用表格的UITableViewCell,以避免不必要的内存开销
static NSString* cellId=@"cellId";
//从可重用表格行的队列中取出一个表格行
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellId];
//如果取出的表格行为nil,如果没有单元格,就创建一个单元格
if (cell==nil) {
switch (indexPath.row%4) {
//4是数组元素个数,实际也是行数
case 0:
//创建一个UITableViewCell对象,使用UITableViewCellStyleSubtitle风格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
break;
case 1:
//创建一个UITableViewCell对象,使用UITableViewCellStyleDefault风格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
break;
case 2:
//创建一个UITableViewCell对象,使用UITableViewCellStyleValue风格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
break;
case 3:
//创建一个UITableViewCell对象,使用UITableViewCellStyleValue风格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
break;
default:
break;
}
}
//将单元格的边框设置为圆角
cell.layer.cornerRadius=12;
cell.layer.masksToBounds=YES;
//从NSIndexPath参数中获取当前行的行号
NSUInteger rowNo=indexPath.row;
//取出_book中索引为rowNo的元素作为UITAbleViewCell的文本标题
cell.textLabel.text=_books[rowNo];
//为UITAbleviewCell左端设置图片
cell.imageView.image=[UIImage imageNamed:@"11.png"];
//为UITAbleViewCell设置左端高亮状态时的图片
cell.imageView.highlightedImage=[UIImage imageNamed:@"12.png"];
//取出_details中所用为rowNo
cell.detailTextLabel.text=_details[rowNo];
return cell;
}
//该方法的返回值决定指定分区内包含多少个表格行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//由于该表格只有各一个分区,直接返回_books中集合元素个数代表行的个数
return _books.count;
}
@end
2.处理单元格的选中
1.AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic,strong)UIWindow *window;
//两个数据源
//作为UITableView显示数据的两个nsarray
@property(nonatomic,strong) NSArray* books;
@property(nonatomic,strong) NSArray* details;
@end
2.AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建并初始化两个nsarray对象
self.books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swifit讲义",@"疯狂ajax讲义"];
self.details=@[@"android的内容还是很复杂的",@"相比安卓,IOS的内容要更容易上手一点",@"swift更新的有点太快了,什么时候能够稳定下来",@"ajax虽然技术上有些古老,但是前端正是火爆的时候"];
return YES;
}
3.ViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "DetailViewController.h"
//控制器类要实现UITAbleViewDataSource和UITAbleViewDelegate协议
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@end
4.ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *table;
@end
@implementation ViewController
{
//定义应用程序委托对象
AppDelegate* _appDelegate;
}
- (void)viewDidLoad {
[super viewDidLoad];
//为UITAbleView控件设置DataSource和delegate
self.table.dataSource=self;
self.table.delegate=self;
_appDelegate=[UIApplication sharedApplication].delegate;//不知道是什么
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear: animated];//调用父类方法
[self.table reloadData];
}
//改方法返回值决定各表格行的控件
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//为表格行定义个静态字符串作为标识符
static NSString* cellId=@"cellId";
//从可重用表格行队列中取出一个表格行
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
//如果取出的表格行为nil
if (cell==nil) {
//创建一个UITableViewCell对象,使用默认风格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
//从indexPath参数中获取当前行的行号
NSUInteger rowNo=indexPath.row;
//取出books中索引为rowno的元素作为UITAbleViewCell的文本标题
cell.textLabel.text=_appDelegate.books[rowNo];
//将单元格的边框设置为圆角
cell.layer.cornerRadius=12;
cell.layer.masksToBounds=YES;
//设置表格单元格左边的图像
cell.imageView.image=[UIImage imageNamed:@"13.png"];
//设置表格单元格左边高亮时的图像
cell.imageView.highlightedImage=[UIImage imageNamed:@"14.png"];
return cell;
}
//重写的方法,该方法返回值决定指定分区内包含多少表格行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//返回books数组的大小即可
return _appDelegate.books.count;
}
//选中表格单元格时
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: