布局:在storyboard中拖入Navigation Controller 控制器,默认后面是tableview,为了使用能自定义tableview.需要新建类继承与uitableviewcontrolle
的Tableviewcontroller,然后在storyboard中指定tableview控制器使用新建的类Tableviewcontroller,可以在新建的Tableviwcontroller.m中来自定义tableview
以项目Music为例
#import "TableViewController.h" #import "MJExtension.h" #import "MUsic.h" #import "PlayingViewController.h" @interface TableViewController () @property(strong,nonatomic)NSArray *musics; @property(strong,nonatomic)PlayingViewController *playingnv; @end @implementation TableViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.musics.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"22222222"); static NSString *ID=@"music"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID]; if(!cell){ cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } MUsic *music=self.musics[indexPath.row]; cell.imageView.image=[UIImage imageNamed:music.singerIcon]; cell.textLabel.text=music.name; cell.detailTextLabel.text=music.singer; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; [self.playingnv show]; } -(NSArray *)musics { if (!_musics) { _musics=[MUsic objectArrayWithFilename:@"Musics.plist"]; } return _musics; } -(PlayingViewController *)playingnv { if (!_playingnv) { self.playingnv=[[PlayingViewController alloc]init]; } return _playingnv; } @end
为了使点击每个cell跳转到相同的xib中
需新建类和xib,继承于uiviewcontroller,为playingviewcontroller
由于上面使用show显示xib里面内容,需在playingviewcontroller.h中新建一个方法
-(void)show;
在playingviewcontroller.m中实现即可
#import "PlayingViewController.h" #import "UIView+Extension.h" @interface PlayingViewController () @end @implementation PlayingViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } -(void)show { // 1. 拿到Window UIWindow *window = [UIApplication sharedApplication].keyWindow; // 2. 设置当前控制器的frame self.view.frame = window.bounds; // 3. 将当前控制器的view添加到Window上 [window addSubview:self.view]; self.view.hidden = NO; // 禁用交互功能 window.userInteractionEnabled = NO; // 4.执行动画, 让控制器的view从下面转出来 self.view.y = window.bounds.size.height; [UIView animateWithDuration:1 animations:^{ // 执行动画 self.view.y = 0; } completion:^(BOOL finished) { // 开启交互 window.userInteractionEnabled = YES; }]; }; @end
原文:http://www.cnblogs.com/lizhan1991/p/4922789.html