#在藍懿學習iOS的日子#Day13

來源:互聯網
上載者:User

標籤:

一、導航控制力器NavigationControeller在第一個 ViewController前添加一個NavigationControeller,建立第二個頁面 SecondViewController1、、在 ViewController.m匯入#import "SecondViewController.h"@implementation ViewController
- (IBAction)leftAction:(UIBarButtonItem *)sender {
        NSLog(@"左按鈕");
}
- (IBAction)clicked:(id)sender {
    SecondViewController *vc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //建立系統樣式按鈕
    UIBarButtonItem *bbi1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightAction)];
   
    //建立文字按鈕
    UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithTitle:@"右按鈕" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
   
//    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bbi1,bbi2, nil];
    self.navigationItem.rightBarButtonItems = @[bbi1,bbi2];
 
   
}
-(void)rightAction{
    NSLog(@"右按鈕");}2、在   SecondViewController.m- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第二個頁面";
   
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(backAction) userInfo:nil repeats:NO];
}
-(void)backAction{
    //跳回上一個頁面
    [self.navigationController popViewControllerAnimated:YES];}二、 UITableView
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *names;
@end

@implementation ViewController
//控制tableView有幾個區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//控制每個區有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
   
    return self.names.count;
}


//控制每行顯示的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
 
   
    //去記憶體中找 有沒有離開頁面的cell 有得話 拿過來直接用 沒有則為nil
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //如果沒有拿到離開頁面的cell則需要建立一個
    if (!cell) {
          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            NSLog(@"%ld-%ld",indexPath.section,indexPath.row);
    }
   
    NSString *name = self.names[indexPath.row];
   
   
    cell.textLabel.text = name;
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.names = [NSMutableArray array];
  
    [self.names addObject:@"劉德華"];
    [self.names addObject:@"張學友"];
    [self.names addObject:@"郭富城"];
   
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
    self.navigationItem.leftBarButtonItem = addItem;
}

-(void)addAction{
    NSString *name = @"王";
    [self.names addObject:name];
    //在ViewDidLoad方法之後 修改資料來源數組的話 需要讓tableView重新載入
    [self.tableView reloadData];
   
   
   }

#在藍懿學習iOS的日子#Day13

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.