Tableview line

- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); On line / /, / / CGContextSetStrokeColorWithColor (context, [UIColor colorWithHexString: @ "FFFFFF"]. CGColor); // CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); / / the divider CGContextSetStrokeColorWithColor (context, UIColorWithRGBA (221, 221, 221, 1). The CGColor); CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1)); }Copy the code

Remove the last dividing line of the cell

If ([[UIDevice currentDevice].SystemVersion floatValue] >= 7.0) {cell. SeparatorInset = UIEdgeInsetsMake(0, -15, 0, 15); } the if ([cell respondsToSelector: @ the selector (setPreservesSuperviewLayoutMargins:)]) {/ / remove the last line of the cell system line [cell setPreservesSuperviewLayoutMargins:NO]; }Copy the code

Gets a row of cells in a group of tableView

NSIndexPath *resonIndex=[NSIndexPath indexPathForRow:0 inSection:1];
UITableViewCell *cell=[tableView cellForRowAtIndexPath:resonIndex];
Copy the code

UIView adds a background image

UIImage *image = [UIImage imageNamed: @" name.png "]; view.layer.contents = (id) image.CGImage; / / if you need transparent background with this view. The layer. The backgroundColor = [UIColor clearColor]. CGColor;Copy the code

Gets what level the navigation bar is in

NSInteger currentIndex = [self.navigationController.childViewControllers indexOfObject:self];

self.navigationController.viewControllers.firstObject
Copy the code

Hide navigation

/ / use this approach not only perfect suit sliding back gesture, but also solves the tabBar switch, dynamic navigation bar hidden problem @ interface ViewController () < UINavigationControllerDelegate > @ end - (void)viewDidLoad { [super viewDidLoad]; / / set the navigation controller agent for self self. The navigationController. Delegate = self; } # pragma mark - UINavigationControllerDelegate / / will display controller - (void) navigationController: (UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { // Epage = [viewController isKindOfClass:[self class]] BOOL isShowHomePage = [viewController isKindOfClass:[self class]]; [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES]; } - (void)dealloc { self.navigationController.delegate = nil; }Copy the code

Go back to the home page and jump to tabbar

self.tabBarController.selectedIndex=[data[@"index"] integerValue];
[self.navigationController popToRootViewControllerAnimated:NO];
Copy the code

Back button Settings

/ / return a certain level of NSArray * array = self. The navigationController. ViewControllers; [self.navigationController popToViewController:array[1] animated:YES]; / / return the superior int index = (int) [[self. The navigationController viewControllers] indexOfObject: self]. [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(index -2)] animated:YES];Copy the code

Destroy the current page while pushing the new page

if ([self.navigationController.childViewControllers indexOfObject:self]! =0) { NSMutableArray *vcs = [self.navigationController.viewControllers mutableCopy]; for (UIViewController *vc in self.navigationController.viewControllers) { if ([vc isKindOfClass:[self class]] ) { [vcs removeObject:vc]; } } self.navigationController.viewControllers=vcs; }Copy the code

Cell reuse prevents reloading/removal of all view controls

for(UIView *view in cell.subviews){ if(view){ [view removeFromSuperview]; }}Copy the code

Cell height cache

@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath; / / cache highly used dictionary - (CGFloat) tableView: (UITableView *) tableView estimatedHeightForRowAtIndexPath: (indexPath NSIndexPath *) { NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath]; if(height) { return height.floatValue; } else { return 100; } } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { NSNumber *height = @(cell.frame.size.height); [self.heightAtIndexPath setObject:height forKey:indexPath]; }Copy the code

Cell sideslip returns to adding a custom image

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } // If you want to display Chinese delete when side pull, Only need to use the following method to replace the above methods is good - (NSArray *) tableView: (UITableView *) tableView editActionsForRowAtIndexPath: (NSIndexPath *)indexPath{ UITableViewRowAction *deleteRoWAction = [UITableViewRowAction RowActionWithStyle: UITableViewRowActionStyleDestructive title: @ "delete" handler: ^ (UITableViewRowAction * action, NSIndexPath *indexPath){//title is self-definable}]; return @[deleteRoWAction]; } //iOS11 after the sideslip delete - support to set the picture With the above don't conflict 】 - (UISwipeActionsConfiguration *) tableView tableView: (UITableView *) TrailingSwipeActionsConfigurationForRowAtIndexPath: (NSIndexPath *) indexPath API_AVAILABLE (ios (11.0)) {/ / deleted UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { DLog(@"delete") completionHandler (YES); }]; deleteRowAction.image = [UIImage imageNamed:@"favor_remove"]; deleteRowAction.backgroundColor = [UIColor whiteColor]; UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]]; return config; }Copy the code

The listening page slides back

- (void)didMoveToParentViewController:(UIViewController *)parent{ [super didMoveToParentViewController:parent]; if(! Parent){// slide back to page time}}Copy the code

Remove all subviews

[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
Copy the code

Gets the coordinate size of the control on the superview

UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
CGRect rect=[testView convertRect:testView.bounds toView:window];
Copy the code

If the price has decimals and is not 0, the number of decimals is displayed; if not, the integer is displayed

- (NSString *)formatPriceWithFloat:(float)price { if (fmodf(price, 1)==0) { return [NSString stringWithFormat:@"%.0f",price]; } else if (fmodf(price*10, 1)==0) {return [NSString stringWithFormat:@"%.1f",price]; } else {// If there are two decimal points return [NSString stringWithFormat:@"%.2f",price]; }}Copy the code

The fmod (), rounding

Add gradients to the view

CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#656565"].CGColor, (__bridge id)[UIColor colorWithHexString:@"#2B2829"].CGColor]; Gradientlayer. locations = @[@0.0, @1.0]; gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(0, 1); gradientLayer.frame = bkView.bounds; [bkView.layer addSublayer:gradientLayer];Copy the code

The sorting

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"5",@"3",@"4",@"2",nil]; // reverse order NSMutableArray *resultArr = (NSMutableArray *)[[array reverseObjectEnumerator] allObjects]; NSLog(@" backorder: %@",resultArr); [array sortUsingSelector:@selector(compare:)]; NSLog(@" ascending: %@",array); NSEnumerator *enumerator = [array reverseObjectEnumerator]; NSMutableArray *downArr = (NSMutableArray *)[enumerator allObjects]; NSLog(@" descending: %@",downArr); // the descending result is: 5,4,3,2Copy the code

Calculate the width by type and text

/ / by calculating width size and copywriting - CGSize completeTextWidthWithFont: (CGFloat) fontSize text: (nsstrings *) text {NSDictionary * attrs = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:fontSize]}; CGSize size = [text sizeWithAttributes:attrs]; return size; }Copy the code

A string becomes a dictionary

NSString * str  = @"{key:value}";
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
Copy the code

View gesture movement

- (void)move:(UIPanGestureRecognizer *)panGesture { if (panGesture.state == UIGestureRecognizerStateChanged) { UIView *subview = panGesture.view; CGPoint point = [panGesture translationInView:self.superview]; CGFloat centerX = subview.centerX + point.x; CGFloat centerY = subview.centerY + point.y; CGFloat left = subview.width / 2.0; CGFloat right = self.superview.width -subview. width / 2.0; CGFloat top = STATUS_NAVI_HEIGHT + subview.height / 2.0; CGFloat top = STATUS_NAVI_HEIGHT + subview.height / 2.0; CGFloat bottom = self.superview. height-subview. height / 2.0-tabbar_height; CGFloat bottom = self.superview. height-subview. height / 2.0-tabbar_height; if (centerX < left) centerX = left; if (centerX > right) centerX = right; if (centerY < top) centerY = top; if (centerY > bottom) centerY = bottom; subview.center = CGPointMake(centerX, centerY); [panGesture setTranslation:CGPointZero inView:self.superview]; }else if (panGesture.state == UIGestureRecognizerStateEnded){ UIView *subview = panGesture.view; CGPoint point = [panGesture translationInView:self.superview]; CGFloat centerX = subview.centerX + point.x; CGFloat centerY = subview.centerY + point.y; CGFloat supCenterX = self. Superview. Width / 2.0; CGFloat top = STATUS_NAVI_HEIGHT + subview.height / 2.0; CGFloat top = STATUS_NAVI_HEIGHT + subview.height / 2.0; CGFloat bottom = self.superview. height-subview. height / 2.0-tabbar_height; CGFloat bottom = self.superview. height-subview. height / 2.0-tabbar_height; If (centerX < supCenterX){centerX = subview.width / 2.0; }else{centerX = self.superview.width -subview.width / 2.0; } if (centerY < top) centerY = top; if (centerY > bottom) centerY = bottom; subview.center = CGPointMake(centerX, centerY); [panGesture setTranslation:CGPointZero inView:self.superview]; }}Copy the code

Unified the singleton

+ (instancetype)sharedInstance {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}


+ (instancetype)shared<#Manager#>
{
    static <#managerClass#> *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[<#managerClass#> alloc] init];
    });
    return shareManager;
}
Copy the code