- 1.計(jì)算有多少組通用公式
示例:服務(wù)器返回15個(gè)數(shù)據(jù),每組8個(gè)
// 15 / 8 = 1
// 15 % 8 > 0 = 1(真)
_pageC.numberOfPages = _mainVM.rowNumber / 8 + (_mainVM.rowNumber % 8 > 0);
- 計(jì)算總共多少行通用公式
NSUInteger count = squares.count;
int totalCols = 4;
unsigned long totalRows = (count + totalCols - 1) / 4;
- 2.自己寫的宏定義要用小k開頭,以區(qū)分系統(tǒng)級(jí)別的宏
#define kShowWarningDelayDuration 1
#define kTimeoutDuration 30
- 3.block 三目運(yùn)算判斷block是否為nil
!block ?: block();
- 4.如何手動(dòng)調(diào)用UICollectionView/tableView的選中cell方法
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
- 5.查找最后一個(gè)逗號(hào),并刪除
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];
// 或者:
NSString *cccc = [cut substringToIndex:[cut length] - 1];
- 6.window的Rect
// 在iOS7以上菲盾,所有控制器默認(rèn)都是全屏的,上面20的狀態(tài)欄高度也屬于控制器
//返回的是帶有狀態(tài)欄的Rect
CGrect screenBounds = [ [UIScreen mainScreen] bounds];
//不包含狀態(tài)欄的Rect
CGRect viewBounds = [ [UIScreen mainScreen] applicationFrame];
- 7.用于去掉cell的分割線左側(cè)15的空隙
- (void)awakeFromNib {
self.separatorInset = UIEdgeInsetsZero;
self.layoutMargins = UIEdgeInsetsZero;
self.preservesSuperviewLayoutMargins = NO;
}
- 8.富文本屬性(修改字符串中指定文字的顏色,字體大小)
//紅色¥ + 黑色數(shù)字
NSDictionary *str1Dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName: [UIColor redColor]};
NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:@"¥" attributes:str1Dic];
NSDictionary *str2Dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor blackColor]};
NSAttributedString *str2 = [[NSAttributedString alloc] initWithString:price attributes:str2Dic];
NSMutableAttributedString *attr = [NSMutableAttributedString new];
[attr appendAttributedString:str1];
[attr appendAttributedString:str2];
_priceLb.attributedText = attr;
- 9.截取路徑最后的部分,非常智能的方法
[self lastPathComponent];```

- 10.過濾
```objc
// break是結(jié)束整個(gè)循環(huán)體装处,continue是結(jié)束單次循環(huán)
// 過濾掉非UITabBarButton
if (![@"UITabBarButton" isEqualToString:NSStringFromClass(subview.class)]) continue;
if (subview.class != NSClassFromString(@"UITabBarButton")) continue;
- 11.按鈕選中狀態(tài)更改三部曲
// 讓上一個(gè)按鈕取消選中狀態(tài)
self.preBtn.selected = NO;
// 讓當(dāng)前按鈕成為選中狀態(tài)
btn.selected = YES;
// 讓當(dāng)前按鈕變成上一個(gè)按鈕
self.preBtn = btn;
- 12.nil,NULL,NSNull區(qū)別
1凑懂、nil:一般賦值給空對象房待;
2倔矾、NULL:一般賦值給nil之外的其他空值。 NULL是C的毙驯,空地址倒堕,地址的數(shù)值是0,是個(gè)長整數(shù);如SEL等爆价;
舉個(gè)栗子(好重啊~):
[NSApp beginSheet:sheet modalForWindow:mainWindow
modalDelegate:nil //pointing to an object
didEndSelector:NULL //pointing to a non object/class
contextInfo:NULL]; //pointing to a non object/class
3垦巴、NSNULL:NSNull只有一個(gè)方法:+ (NSNull *) null;
[NSNull null]用來在NSArray和NSDictionary中加入非nil(表示列表結(jié)束)的空值.
4、當(dāng)向nil發(fā)送消息時(shí)铭段,返回NO骤宣,不會(huì)有異常,程序?qū)⒗^續(xù)執(zhí)行下去序愚;
而向NSNull的對象發(fā)送消息時(shí)會(huì)收到異常憔披。
- 13.當(dāng)一個(gè)控件添加自己的約束時(shí)候,必須設(shè)置系統(tǒng)的autorezing為NO;
self.btn.translatesAutoresizingMaskIntoConstraints = NO;
- 14.SDWebImage 有兩個(gè)宏 來判斷程序在主線程運(yùn)行
// 需要導(dǎo)入#import "EMSDWebImageCompat.h"
#define dispatch_main_sync_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}
#define dispatch_main_async_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}
// 使用:
dispatch_main_async_safe((^{
self.headIcon.image = [UIImage imagenamed:fsf];
}));
15.
UIApplicationUserDidTakeScreenshotNotification
通知,當(dāng)用戶截屏?xí)r觸發(fā)
UIApplicationDidBecomeActiveNotification
通知 ,程序從后臺(tái)進(jìn)入前臺(tái)的
應(yīng)用場景示例:掃描二維碼的動(dòng)畫進(jìn)入后臺(tái)后會(huì)執(zhí)行completion 的block,可以利用這個(gè)通知再次調(diào)用開始動(dòng)畫16.手機(jī)振動(dòng)
- (void)vibrate {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
- 17.一個(gè)View中三個(gè)按鈕,獲取點(diǎn)擊按鈕的索引,—>監(jiān)聽點(diǎn)擊的是哪個(gè)按鈕
NSUInteger index = [button.superview.subViews indexOfObject:button];
示例如下圖
Snip20160522_7.png
- 18.IBOutletCollection(ClassName)使用這個(gè)偽關(guān)鍵字展运,可以將界面上一組相同的控件連接到同一個(gè)數(shù)組中
偽關(guān)鍵字的定義活逆,可以從UIKit.framework的頭文件UINibDeclarations.h找到如下定義:
#ifndef IBOutletCollection
#define IBOutletCollection(ClassName)
#endif
在Clang源碼中精刷,有更安全的定義方式,如下所示:
#define IBOutletCollection(ClassName) __attribute__((iboutletcollection(ClassName)))
- 不過在使用IBOutletCollection時(shí)蔗候,需要注意兩點(diǎn):
- IBOutletCollection集合中對象的順序是不確定的怒允。我們通過調(diào)試方法可以看到集合中對象的順序跟我們連接的順序是一樣的。但是這個(gè)順序可能會(huì)因?yàn)椴煌姹镜腦code而有所不同锈遥。所以我們不應(yīng)該試圖在代碼中去假定這種順序纫事。
- 不管IBOutletCollection(ClassName)中的控件是什么,屬性的類型始終是NSArray所灸。實(shí)際上丽惶,我們可以聲明是任何類型,如NSSet爬立,NSMutableArray钾唬,甚至可以是UIColor,但不管我們在此設(shè)置的是什么類侠驯,IBOutletCollection屬性總是指向一個(gè)NSArray數(shù)組抡秆。
Snip20160522_9.png
- 19.去掉BackBarButtonItem的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
- 20.去掉UITableView風(fēng)格為group時(shí)候的最頂部的空白距離
CGFLOAT_MIN 這個(gè)宏表示 CGFloat 能代表的最接近 0 的浮點(diǎn)數(shù),吟策,64 位下大概是 0.00(300左右個(gè))0225 這個(gè)樣子
這樣寫單純的為了避免一個(gè)魔法數(shù)字儒士,這里用 0.1 效果是一樣的
CGRect frame=CGRectMake(0, 0, 0, CGFLOAT_MIN);
self.tableView.tableHeaderView=[[UIView alloc]initWithFrame:frame];
// 或
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) return CGFLOAT_MIN;
return 6;
}