iOS

Skills

exit the keyboard

- (void)touchesBegan:(NSSet<UITouch *> *)touches     withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

Add Target to control component

[self.datePicker addTarget:self action:@selector(getInput:) forControlEvents:UIControlEventValueChanged];

DatePicker

set frame after DatePickerStyle to gurantee datepicker in center

PickerView

Implement UIPickerViewDelegate & UIPickerViewDataSource in Interface

@interface ViewController()<UIPickerViewDelegate, UIPickerViewDataSource>
@end

functions

component = cols index
row = rows index

  1. cols number

-(NSInteger)numberOfComponentsInPickerView:
2. rows number
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
3. title of every row
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
4. when select each row
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

set delegate

self.pickerView.delegate = self;
self.pickerView.dataSource = self; 

ScrollView

Three Attributes

  1. contentSize - the size of scrolling content
  2. contentOffset - the offset between the pos with [0,0]
  3. contentInset - edge size

Important Delegate Methods

// use when scrolling
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

// use when zooming
- (void)scrollViewDidZoom:(UIScrollView *)scrollView API_AVAILABLE(ios(3.2));

// start draging
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

// will end draging
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset API_AVAILABLE(ios(5.0));

// end drag
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

// start decelerating
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;

// end decelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
// zoom component
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
// start zooming
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view;
// finish zooming
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale;

option + mouse = finger gesture

Get bottom line of the most bottom component

CGRectGetMaxY(component.frame)

#import "ViewController.h"

@interface ViewController()<UIScrollViewDelegate>
@property(strong, nonatomic) UIScrollView *scrollView;
@property(strong, nonatomic) UIPageControl *pageControl;
@end

@implementation ViewController
CGFloat width;
-(void)viewDidLoad {
[super viewDidLoad];

width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, height, 414, 200)];
self.scrollView.delegate = self;
self.scrollView.backgroundColor = [UIColor blueColor];

for(int i = 0; i < 5; i++) {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, 200)];
    if (i%2 == 0) {
        imgView.image = [UIImage imageNamed:@"1"];
    } else {
        imgView.image = [UIImage imageNamed:@"I-797C.jpeg"];
    }

    [self.scrollView addSubview:imgView];
}

self.scrollView.contentSize = CGSizeMake(5*width, 0);
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.pagingEnabled = YES;



self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width/2 - 100, 190, 200, 20)];
self.pageControl.numberOfPages = 5;
self.pageControl.currentPage = 0;
self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
self.pageControl.backgroundStyle = 2;

//self.pageControl
[self.view addSubview:self.scrollView];
[self.view addSubview:self.pageControl];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat offset = self.scrollView.contentOffset.x;

self.pageControl.currentPage = offset / width;
}

@end

TableView

Structure

Function

Delete Row

UI & Implementation
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.data removeObjectAtIndex:indexPath.row];

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"改变";
}

Insert Row

UI

button click

implementation
-(void) addNew:(UIButton *) button {
[self.tableView setEditing:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [self.data removeObjectAtIndex:indexPath.row];

    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
    [self.data insertObject:@"iPhone" atIndex:indexPath.row];

    //[self.tableView reloadData];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

}

Move Row

UI & Implementation
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSString *temp = self.data[sourceIndexPath.row];
[self.data removeObjectAtIndex:sourceIndexPath.row];

[self.data insertObject:temp atIndex:destinationIndexPath.row];
}

Index design

Methods
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.sectionTitles;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSLog(@"%d = %@", index, title);

return index;

}

self design UITableViewCell

click here

static UITabelView

click here

Refreshing

1.

UIRefreshControl *control = [[UIRefreshControl alloc] init];
control.attributedTitle = [[NSAttributedString alloc] initWithString:@"refreshing..."];
control.tintColor = [UIColor redColor];

[control addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];

self.tableView.refreshControl = control;

2.

-(void) refreshTableView {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self.data insertObject:@"123" atIndex:0];

    [self.tableView reloadData];
    if ([self.tableView.refreshControl isRefreshing])
        [self.tableView.refreshControl endRefreshing];
});
} 

CollectionView

click here

self-design collectionViewCell

click here

ViewController

launch

click here

process

Contents
  1. 1. Skills
    1. 1.1. exit the keyboard
    2. 1.2. Add Target to control component
    3. 1.3. DatePicker
    4. 1.4. PickerView
      1. 1.4.1. Implement UIPickerViewDelegate & UIPickerViewDataSource in Interface
      2. 1.4.2. functions
      3. 1.4.3. set delegate
    5. 1.5. ScrollView
      1. 1.5.1. Three Attributes
      2. 1.5.2. Important Delegate Methods
        1. 1.5.2.1. Zoom related Method
        2. 1.5.2.2. Get bottom line of the most bottom component
        3. 1.5.2.3. Example Carousel
    6. 1.6. TableView
      1. 1.6.1. Structure
      2. 1.6.2. Function
        1. 1.6.2.1. Delete Row
          1. 1.6.2.1.1. UI & Implementation
        2. 1.6.2.2. Insert Row
          1. 1.6.2.2.1. UI
          2. 1.6.2.2.2. implementation
        3. 1.6.2.3. Move Row
          1. 1.6.2.3.1. UI & Implementation
        4. 1.6.2.4. Index design
          1. 1.6.2.4.1. Methods
        5. 1.6.2.5. self design UITableViewCell
        6. 1.6.2.6. static UITabelView
        7. 1.6.2.7. Refreshing
    7. 1.7. CollectionView
      1. 1.7.1. self-design collectionViewCell
    8. 1.8. ViewController
      1. 1.8.1. launch
      2. 1.8.2. process
|