judaco / iPhone

0 stars 0 forks source link

iPhone - Class 09 #5

Open judaco opened 7 years ago

judaco commented 7 years ago
//
//  ViewController.m
//  TableView
//
//  Created by hackeru on 14/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UITableView * tableView;
    NSMutableArray<NSString*> * animals;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    animals = [[NSMutableArray alloc] init];
    for (int i = 1; i <100 ; i++) {
        [animals addObject:[NSString stringWithFormat:@"animal %i", i]];
    }

    tableView = [[UITableView alloc] initWithFrame: self.view.frame style:UITableViewStylePlain];
    [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"identifier"];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return animals.count;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [animals removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
-(BOOL)prefersStatusBarHidden{
    return YES;
}
         -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

             UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
             cell.textLabel.text = animals[indexPath.row];

             return cell;
         }
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago
//
//  ViewController.m
//  TableView Header and Footer
//
//  Created by hackeru on 14/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UITableView * tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    [tableView registerClass: UITableViewCell.class forCellReuseIdentifier:@"identifier"];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"Section %li Row %li", indexPath.section, indexPath.row];

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(UILabel *) labelWithTitle:(NSString *)title{
    UILabel * label = [[UILabel alloc] init];
    label.text = title;
    label.backgroundColor = [UIColor greenColor];
    [label sizeToFit];
    return label;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return [self labelWithTitle:[NSString stringWithFormat:@"Section %li Header", section]];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [self labelWithTitle:[NSString stringWithFormat:@"Section %li Footer", section]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago
//
//  ViewController.m
//  TableView Refresh
//
//  Created by hackeru on 14/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UITableView * tableView;
    NSMutableArray<NSDate*> *dates;
    UIRefreshControl * refreshControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    dates = [[NSMutableArray alloc] init];
    [dates addObject:[NSDate date]];
    tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"identifier"];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];

    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:NSSelectorFromString(@"handleRefresh:") forControlEvents:UIControlEventValueChanged];
    [tableView addSubview:refreshControl];
}
-(void)handleRefresh:(UIRefreshControl *)sender{

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dates.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    NSDate * date;
    cell.textLabel.text = [date description];

    return cell;
}
-(BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end