OC小结
2017-04-14 本文已影响25人
Jackson_Z
禁止第三方输入键盘
//禁止第三方输入键盘
- (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];
}
}