Navigation 与 tableview controller 一起使用

这篇博客介绍了如何在iOS应用中将Navigation与Table View Controller结合使用,创建了一个使用push segue从Table View Controller到另一个UIViewController的示例,并详细解释了相关代码实现,包括设置cell的identifier、初始化tableViewCell以及实现 segue 跳转的回调方法。
摘要由CSDN通过智能技术生成

新建程序 到story board 中 拖个Navigation 进来  默认Navigation 的root controller  是table view controller 我们可以改成别的 上章就是改成最普通的 view controller  这次我们就以table view controller 做根界面 所以可以不用改 

然后再拖个 uiviewcontroller 进来 做具体显示界面


点击  table view 中的 cell 拉线到other controller 选择 push


然后我们直接设置 cell 中的一些属性



identifier 记得要填上  要不然不会 cell 不会实现 push segue

新建 一个 tablecontoller  继承 UITableViewController  

UITableViewController 默认把Controller中的 TableView 的 dataSource 和delegate 事件 绑定到自身中  


如果 是你自己拖的 UITableView  可以在界面上 直接拖绑定 也可以用代码写

    tableView.delegate =self;

    tableView.dataSource =self;

self  可以是任意对象  一般为了根好了解这类可以做什么  我们可以这样写 来表达 这类所实现的借口

#import<UIKit/UIKit.h>


@interface testViewController :UITableViewController<UITableViewDelegate,UITableViewDataSource>


@end

<>里面我们实现的借口  多个用,号 隔开
 object-c 中没有强制实现借口的要求

下面是代码

//

//  testViewController.m

//  test

//

//  Created by mac on 12-4-23.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import"testViewController.h"

#import"otherController.h"

@interfacetestViewController ()

{

    NSArray* array;

}

@end

@implementation testViewController

- (void)viewDidLoad

{

   array = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidUnload

{

    [superviewDidUnload];

   // Release any retained subviews of the main view.

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);

}

//返回组的数量

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}

//返回section组的子项数量

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return array.count;

}

//返回当前分组的组名

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return [NSString stringWithFormat:@"%d",section];

}

//返回具体的内容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString* CellIdentifier =@"Cell"; //可以随便填  tableview 通过这个identifier 来复用你的控件 如果你有跳转的一些事件  要跟界面上的一致

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)

    {   

       //初始化tableviewcell 主要是 style  显示的样式

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

   //设置 内容

    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

    return  cell;

}

//通过 segue跳转的 回调方法

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if([segue.identifier isEqualToString:@"TableCellToOther"])

    {

        NSIndexPath* path = [self.tableView indexPathForSelectedRow];

        NSInteger row = path.row;

        NSString* title = [NSString stringWithFormat:@"%d组,%@",path.section,[array objectAtIndex:row]];

       //可以通过 segue.destinationviewcontroller获得跳转的下一个controller  可以直接调用其方法 

       //记得 加上头文件      #import "otherController.h"

        [segue.destinationViewController showTitle:title];

    }

}

@end


在othercontroller 就加个显示标题的方法 给前面的调用

-(void)showTitle:(NSString *)title

{

    self.title = title;

}

运行界面




其实 你在建立项目时选择 master-detail application   你会发现里面的效果是差不多的。  像翻页的效果可以建个 page based application 自己看下里面的实现  苹果很多动画都是封装好的  调用很方便  至少我现在觉得比 android 动画效果好看也容易实现

    


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值