OC小结

作者: Jackson_Z | 来源:发表于2017-04-14 18:37 被阅读25次

    禁止第三方输入键盘

    //禁止第三方输入键盘
    - (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier {
        return NO;
    }
    

    行数算法

    //行数
    NSInteger row = (self.btnArr.count + colNum - 1) / colNum;
    

    九宫格算法

    -(void)layoutSubviews{
        [super layoutSubviews];
        //设置按钮的frame值
        //计算处在哪行哪列
        for (int i = 0; i < self.btnArr.count; ++i) {
            
            NSInteger col = i % colNum;
            NSInteger row = i / colNum;
            
            CGFloat btnX = padding + col * (tangButtonW + margin);
            CGFloat btnY = 44 + padding + row * (tangButtonH + margin);
            //设置按钮的frame值
            UIButton *btn = self.btnArr[i];
            btn.frame = CGRectMake(btnX, btnY, tangButtonW, tangButtonH);
        }
    }
    

    处理连点

    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:btn];
    [self performSelector:@selector(todoSomething:) withObject:btn afterDelay:0.2f];
    

    IMP类型(就是实现方法) 来源:UITableView-FDTemplateLayoutCell-master

    - (IBAction)rightNavigationItemAction:(id)sender {
        [[[UIActionSheet alloc]
          initWithTitle:@"Actions"
          delegate:self
          cancelButtonTitle:@"Cancel"
          destructiveButtonTitle:nil
          otherButtonTitles:
          @"Insert a row",
          @"Insert a section",
          @"Delete a section", nil]
         showInView:self.view];
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        SEL selectors[] = {
            @selector(insertRow),
            @selector(insertSection),
            @selector(deleteSection)
        };
    
        if (buttonIndex < sizeof(selectors) / sizeof(SEL)) {
            //定义IMP类型,IMP类型就是实现方法
            void(*imp)(id, SEL) = (typeof(imp))[self methodForSelector:selectors[buttonIndex]];
            imp(self, selectors[buttonIndex]);
        }
    }
    
    - (FDFeedEntity *)randomEntity {
        NSUInteger randomNumber = arc4random_uniform((int32_t)self.prototypeEntitiesFromJSON.count);
        FDFeedEntity *randomEntity = self.prototypeEntitiesFromJSON[randomNumber];
        return randomEntity;
    }
    
    - (void)insertRow {
        if (self.feedEntitySections.count == 0) {
            [self insertSection];
        } else {
            [self.feedEntitySections[0] insertObject:self.randomEntity atIndex:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    
    - (void)insertSection {
        [self.feedEntitySections insertObject:@[self.randomEntity].mutableCopy atIndex:0];
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    - (void)deleteSection {
        if (self.feedEntitySections.count > 0) {
            [self.feedEntitySections removeObjectAtIndex:0];
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    

    相关文章

      网友评论

          本文标题:OC小结

          本文链接:https://www.haomeiwen.com/subject/ijkgattx.html