先看一下示例程序
其中涉及到的用法有:
创建项目什么的我就不再赘述了,下面直接贴代码
先创建一个ViewController类型的类。名字叫SecondViewController
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//声明一个nsstring类型的属性,便于之后的属性传值
@property (nonatomic,strong)NSString * type;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
//点击跳转过后,tabbar消失,点击返回后出现
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibNameOrNil];
if (self) {
self.hidesBottomBarWhenPushed = YES;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([_type isEqualToString:@"101"]) {
self.view.backgroundColor = [UIColor whiteColor];
}else if ([_type isEqualToString:@"102"])
{
self.view.backgroundColor = [UIColor redColor];
}else if ([_type isEqualToString:@"103"])
{
self.view.backgroundColor = [UIColor yellowColor];
}else if ([_type isEqualToString:@"104"])
{
self.view.backgroundColor = [UIColor greenColor];
}else if ([_type isEqualToString:@"105"])
{
self.view.backgroundColor = [UIColor blueColor];
}
else
{
self.view.backgroundColor = [UIColor grayColor];
}
}
@end
//设置跟视图的背景色为白色(此举是为了避免页面跳转的时候出现页面显示问题,待会放图)
self.window.backgroundColor = [UIColor whiteColor];
//初始化首页
ViewController * VC = [[ViewController alloc]init];
//新建一个navigationcontroller并初始化
//新建底部菜单栏
UITabBarController * tabBar = [[UITabBarController alloc]init];
//把首页导航栏添加到tabbar中,如果有第二个第三个页面,也可以依次在[]中添加,并且以“,”隔开
tabBar.viewControllers = @[nav];
//把tabbar设置为跟视图
self.window.rootViewController = tabBar;
**注意:**上面提到,如果不设置window的背景色,会出现页面跳转的显示问题,大家看一下区别
有没有发现?导航栏右边会有一个黑色模糊的区域,这是非常难看的
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * mainTableView;
@end
@implementation ViewController
-(id)init
{
if (self = [super init]) {
self.title = @"首页";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_mainTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
//去除多余的cell分割线
_mainTableView.tableFooterView = [[UIView alloc]init];
// _mainTableView.backgroundColor = [UIColor yellowColor];
// self.view.backgroundColor = [UIColor redColor];
[self.view addSubview:_mainTableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}else if (section == 1)
{
return 2;
}
else
{
return 3;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * ID = @"My cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// cell.backgroundColor = [UIColor grayColor];
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section, (long)indexPath.row];
//设置cell右侧的指示箭头,表示点击还有页面跳转
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置点击cell的时候能立即恢复原来的状态而不是点击一下立即变成灰色
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//属性传值,必须先初始化,值传递过去之后就不能再次初始化了,因为这个传递过程中是带参数的,再次初始化过后就没了
SecondViewController * secVC = [[SecondViewController alloc]init];
secVC.title = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section,indexPath.row];
//带有导航栏的跳转方式,没有导航栏的不能这么写
[self.navigationController pushViewController:secVC animated:YES];
if (indexPath.section == 0) {
secVC.type = @"101";
}else if (indexPath.section == 1)
{
if (indexPath.row == 0) {
secVC.type = @"102";
}else
{
secVC.type = @"103";
}
}
else
{
if (indexPath.row == 0) {
secVC.type = @"104";
}
else if (indexPath.row == 1)
{
secVC.type = @"105";
}
else
{
secVC.type = @"106";
}
}
}
}
@end
在ViewController中我注解掉的代码,有兴趣的朋友可以取消注解运行一下看看是什么效果。
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:UINavigation导航栏和UITableviewcell点击事件写法