積少成多错维,每天進(jìn)步一點(diǎn)點(diǎn)钦勘,2016/5/10
Xcode控制臺(tái)字體為SF Mono
1--xib約束自適應(yīng)
xib或者系統(tǒng)的cell局部空間需要自適應(yīng)時(shí),使用下面代碼复濒,將IB中的約束當(dāng)做屬性拖出來(lái),然后利用以下語(yǔ)句動(dòng)態(tài)給其賦值
CGSize sizeFit = [self.primeCostLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
//動(dòng)態(tài)計(jì)算測(cè)量
CGSize sizeFit = [cell.nameLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
cell.nameLabelWidth.constant = sizeFit.width;
2--通過(guò)點(diǎn)擊的控件獲取對(duì)應(yīng)的cell的index
例如每個(gè)cell上有個(gè)button,用以下方法:
//
// HGManageOrdersTableViewCell *cell = (HGManageOrdersTableViewCell * )[senderButton superview].superview;
// NSIndexPath *indexPath = [_myFirstTableView indexPathForCell:cell];
// NSLog(@"發(fā)貨按鈕對(duì)應(yīng)index%ld",(long)indexPath.section);
3--給imageView添加手勢(shì)
imageView用戶(hù)交互默認(rèn)是關(guān)閉的纽疟,需手動(dòng)打開(kāi)
cell.firstAddImageView.userInteractionEnabled = YES;
4--iOS富文本(指定區(qū)間的文字變色,變字形等)
NSMutableAttributedString *nowPrice = [[NSMutableAttributedString alloc]initWithString:@"現(xiàn)價(jià):¥"];
[nowPrice addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 1)];
cell.nowPriceLabel.attributedText = nowPrice;
5--判斷點(diǎn)擊點(diǎn)是否在某一區(qū)域內(nèi)
//矩形區(qū)間判斷點(diǎn)擊點(diǎn)范圍
/*
// //點(diǎn)擊點(diǎn)(在tableview中)
// CGPoint tableViewPoint = [sender locationInView:self.myTableView];
//
// CGRect rect00 = [oneCell convertRect:oneCell.firstAddImageView.frame toView:self.myTableView];
// if (CGRectContainsPoint(rect00, tableViewPoint)) {
// NSLog(@"第一行,第一個(gè)");
// [self addPicture:oneCell withIndex:0];
// return;
// }
*/
6--等待某一事件執(zhí)行完成之后再執(zhí)行其他
有時(shí)候系統(tǒng)的方法在for循環(huán)里憾赁,即使用線(xiàn)程也沒(méi)有辦法保證優(yōu)先走完整個(gè)循環(huán)再執(zhí)行后邊的污朽,以下辦法親測(cè)有效
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (NSURL *url in self.finalImagesArray) {
dispatch_async(queue, ^{
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
UIImage * image = [UIImage imageWithCGImage:asset.aspectRatioThumbnail];
NSData *data = [HGRequestManager reSizeImageData:image maxImageSize:800 maxSizeWithKB:1024.0];
NSString *string = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
[imageArray addObject:string];
NSLog(@"轉(zhuǎn)碼中...");
dispatch_semaphore_signal(sema);
}failureBlock:^(NSError *error) {
dispatch_semaphore_signal(sema);
NSLog(@"error=%@",error);
}];
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
7--下拉刷新與上拉加載
// 1.下拉刷新
_tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 模擬延遲加載數(shù)據(jù),因此2秒后才調(diào)用(真實(shí)開(kāi)發(fā)中龙考,可以移除這段gcd代碼)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 結(jié)束刷新
[_tableView.mj_header endRefreshing];
});
}];
// 設(shè)置自動(dòng)切換透明度(在導(dǎo)航欄下面自動(dòng)隱藏)
_tableView.mj_header.automaticallyChangeAlpha = YES;
// 2.上拉加載
_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
// 模擬延遲加載數(shù)據(jù)蟆肆,因此2秒后才調(diào)用(真實(shí)開(kāi)發(fā)中,可以移除這段gcd代碼)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 結(jié)束刷新
[_tableView.mj_footer endRefreshing];
});
}];
8--二維碼掃描識(shí)別區(qū)域問(wèn)題
//設(shè)置rectOfInterest///CGRectMake(y的起點(diǎn)/屏幕的高晦款,x的起點(diǎn)/屏幕的寬炎功,掃描的區(qū)域的高/屏幕的高,掃描的區(qū)域的寬/屏幕的寬)
// //掃描框
_boxView = [[UIView alloc]init];
_boxView.frame = CGRectMake(0, 0,SCREEN_WIDTH * 0.6, 120);
_boxView.center = self.view.center;
// _boxView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];
_boxView.layer.borderColor = [UIColor redColor].CGColor;
_boxView.layer.borderWidth = 0.5f;
[_scanView addSubview:_boxView];
outPut.rectOfInterest = CGRectMake(_boxView.frame.origin.y/SCREEN_HEIGHT, _boxView.frame.origin.x/SCREEN_WIDTH, _boxView.frame.size.height/SCREEN_HEIGHT, _boxView.frame.size.width/SCREEN_WIDTH);
9--使用貝塞爾曲線(xiàn)實(shí)現(xiàn)button圓角
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
10--設(shè)置日期時(shí)的dateFormatter代號(hào)
紀(jì)元的顯示:
G:顯示AD缓溅,也就是公元
年的顯示:
yy:年的后面2位數(shù)字
yyyy:顯示完整的年
月的顯示:
M:顯示成1~12蛇损,1位數(shù)或2位數(shù)
MM:顯示成01~12,不足2位數(shù)會(huì)補(bǔ)0
MMM:英文月份的縮寫(xiě),例如:Jan
MMMM:英文月份完整顯示淤齐,例如:January
日的顯示:
d:顯示成1~31股囊,1位數(shù)或2位數(shù)
dd:顯示成01~31,不足2位數(shù)會(huì)補(bǔ)0
星期的顯示:
EEE:星期的英文縮寫(xiě)床玻,如Sun
EEEE:星期的英文完整顯示毁涉,如,Sunday
上/下午的顯示:
aa:顯示AM或PM
小時(shí)的顯示:
H:顯示成0~23锈死,1位數(shù)或2位數(shù)(24小時(shí)制
HH:顯示成00~23贫堰,不足2位數(shù)會(huì)補(bǔ)0(24小時(shí)制)
K:顯示成0~12,1位數(shù)或2位數(shù)(12小時(shí)制)
KK:顯示成0~12待牵,不足2位數(shù)會(huì)補(bǔ)0(12小時(shí)制)
分的顯示:
m:顯示0~59其屏,1位數(shù)或2位數(shù)
mm:顯示00~59,不足2位數(shù)會(huì)補(bǔ)0
秒的顯示:
s:顯示0~59缨该,1位數(shù)或2位數(shù)
ss:顯示00~59偎行,不足2位數(shù)會(huì)補(bǔ)0
S: 毫秒的顯示
時(shí)區(qū)的顯示:
z / zz /zzz :PDT
zzzz:Pacific Daylight Time
Z / ZZ / ZZZ :-0800
ZZZZ:GMT -08:00
v:PT
vvvv:Pacific Time
11--GCD網(wǎng)站
http://www.samirchen.com/ios-gcd/
12--iOS坐標(biāo)轉(zhuǎn)換
iOS-- UIView中的坐標(biāo)轉(zhuǎn)換
// 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 將像素point從view中轉(zhuǎn)換到當(dāng)前視圖中贰拿,返回在當(dāng)前視圖中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中蛤袒,返回在目標(biāo)視圖view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 將rect從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到 controllerA中
// controllerA 中有一個(gè)UITableView, UITableView里有多行UITableVieCell膨更,cell上放有一個(gè)button
// 在controllerA中實(shí)現(xiàn):
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc為btn在controllerA中的rect
或當(dāng)已知btn時(shí):
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
版權(quán):轉(zhuǎn)自http://blog.csdn.net/xuhuan_wh/article/details/8486337
13--為系統(tǒng)類(lèi)添加屬性
新建時(shí)選擇category
@interface NSString (Ass)
給NSString類(lèi)添加兩種類(lèi)型的屬性, 字符串類(lèi)型的tag值strFlag, 以及int型的tag值intTag.
定義這個(gè)兩個(gè)屬性的set和get方法:
// 對(duì)象屬性的set和get
- (void)setStrFlag:(NSString *)strFlag;
- (NSString *)strFlag;
// 非對(duì)象屬性的set和get
- (void)setIntFlag:(int)intFlag;
- (int)intFlag;
@end
實(shí)現(xiàn)這四個(gè)方法:
需導(dǎo)入runtime頭文件:
#import <objc/runtime.h>
@implementation NSString (Ass)
static int _intFlag;
static NSString *_strFlag;
- (void)setStrFlag:(NSString *)flag {
// void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
objc_setAssociatedObject(self, &_strFlag, flag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)strFlag {
// id objc_getAssociatedObject(id object, const void *key)
return objc_getAssociatedObject(self, &_strFlag);
}
- (void)setIntFlag:(int)intFlag {
NSNumber *t = @(intFlag);
// void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
objc_setAssociatedObject(self, &_intFlag, t, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (int)intFlag {
// id objc_getAssociatedObject(id object, const void *key)
NSNumber *t = objc_getAssociatedObject(self, &_intFlag);
return (int)[t integerValue];
}
@end
14--去掉tableViewCell的下方分割線(xiàn)
- (void)addSubview:(UIView *)view{
if (![view isKindOfClass:[NSClassFromString(@"_UITableViewCellSeparatorView") class]] && view){
[super addSubview:view];
}
}
15--Xcode出現(xiàn)The file couldn’t be opened
一般是因?yàn)樵诖a合并時(shí)出現(xiàn)沖突妙真,右鍵showInFinder打開(kāi)工程藍(lán)色project文件,
然后在右鍵選擇色顯示包內(nèi)容荚守,打開(kāi)
project.pbxproj
文件 珍德,command + F查找===
,16--首次提交代碼到git倉(cāng)庫(kù)時(shí)報(bào)錯(cuò)
一般是由于在git上新建項(xiàng)目的時(shí)候加了MIT許可證,導(dǎo)致本地與遠(yuǎn)程沖突,解決辦法,強(qiáng)制提交git push -u origin master -f
或者 git pull origin master --allow-unrelated-histories
17--npm start項(xiàng)目啟動(dòng)報(bào)錯(cuò)
目前遇到的一次是因?yàn)橛形募薷牧藳](méi)有提交敞贡,用git stash 或者git add后就可以了泵琳,其他情況遇到再說(shuō)
18--布局時(shí)tableView向下彈64
如果上下邊邊界選擇的是mas_bottomLayoutGuide
以及mas_topLayoutGuide
,頁(yè)面出現(xiàn)后就會(huì)向下彈64嫡锌,解決辦法是下邊界不要直接去以頁(yè)面為準(zhǔn)虑稼,可以參考另一個(gè)控件配置,比如在tableView下面放一個(gè)提交按鈕.
原因暫時(shí)不明:
-(UITableView *)orderTableView{
if (!_orderTableView) {
_orderTableView = [UITableView new];
[self.view addSubview:_orderTableView];
_orderTableView.delegate = self;
_orderTableView.dataSource = self;
[_orderTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
make.width.mas_equalTo(self.view);
make.centerX.mas_equalTo(self.view);
make.bottom.mas_equalTo(self.submitButton.mas_top).with.offset(-10);
}];
}
return _orderTableView;
}
19--SQLITE中exists的用法
一種通俗的可以理解為:將外查詢(xún)表的每一行势木,代入內(nèi)查詢(xún)作為檢驗(yàn)蛛倦,如果內(nèi)查詢(xún)返回的結(jié)果取非空值,則EXISTS子句返回TRUE啦桌,這一行行可作為外查詢(xún)的結(jié)果行溯壶,否則不能作為結(jié)果及皂。
20--sqlite 復(fù)制表
【復(fù)制表結(jié)構(gòu)及數(shù)據(jù)】
CREATE TABLE NEW_TABLE(新表) AS SELECT * FROM (OLD_TABLE);
【僅僅復(fù)制結(jié)構(gòu),不復(fù)制數(shù)據(jù)】
CREATE TABLE NEW_TABLE AS SELECT * FROM WHERE 1=2;
,跟上面的相比且改,加了where語(yǔ)句并將條件設(shè)為假
21--CKContainer獲取iCloud賬號(hào)返回值不準(zhǔn)確
在iOS設(shè)備明明有賬號(hào)登錄验烧,返回值卻是沒(méi)有賬號(hào)登錄,原因是設(shè)置中iCloud中iCloud Drive沒(méi)有針對(duì)該app打開(kāi)又跛,導(dǎo)致iCloud不能正常工作
22--直接判斷網(wǎng)絡(luò)是否連接
+ (BOOL)connectedToNetwork
{
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flags\n");
return NO;
}
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
return (isReachable && !needsConnection) ? YES : NO;
}