1.按鈕多選循環(huán)字典通過tag判斷
點擊下一步時字典記得初始化
for (NSInteger i = 10; i < _arraycount1.count +10; i++) {
UIButton *tempButton = (UIButton *)[self.view viewWithTag:100 + i - 10];
if (btn.tag == i) {
for (NSString *key in self.duoxuandic.allKeys) {
if ([key integerValue ] == i) {
if ([self.duoxuandic[key]integerValue] == 0) {
[tempButton setImage:[UIImage imageNamed:@"選中-30.png"] forState:UIControlStateNormal];
[_arr addObject:_arraycount2[i-10]];
self.duoxuandic[key] = [NSNumber numberWithBool:YES];
}else{
[tempButton setImage:[UIImage imageNamed:@"未選中-30.png"] forState:UIControlStateNormal];
self.duoxuandic[key] = [NSNumber numberWithBool:NO];
[_arr removeLastObject];
}
}
}
}
2.把數(shù)組里的元素以字符串形式展現(xiàn)出來并且用逗號隔開
NSString *string = [array componentsJoinedByString:@","]
取出字符串下標前后的字符串
[string substringToIndex:7];
substringFromIndex
//截取下標6后一位的字符串
[sr substringWithRange:NSMakeRange(6, 1)]
3.self.automaticallyAdjustsScrollViewInsets = NO;防止tableView的cell上移下移
4.每次進入頁面刷新數(shù)據(jù)流程
[_arrayCount removeAllObjects];
[self request];
5.tableViewcell位置竄動
當 tableView 的內(nèi)容比較多時底部的內(nèi)容反而顯示不下躺彬。這就很奇怪了宪拥,按照前面的結(jié)論江解,這時候 tableView是從導航欄底部開始布局的徙歼,contentInset 也是(0鳖枕,0宾符,0,0)辣苏,怎么底部的內(nèi)容會被遮擋一部分呢哄褒?原因在于self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
初始化時 rootView 的 frame 還是(0呐赡,0,screenWidth萌狂,screenHeight)茫藏,只需要在viewWillLayoutSubviews
中重新修改一下 tableview 的 frame 即可霹琼,
- (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; _tableView.frame = self.view.bounds;
}
6.提取App的UI素材
打開iTunes,在App Store下載覺得UI不錯的App树灶,下載完成以后可以在我的應用中看到App天通。將App直接拖拽到桌面,得到App的ipa文件烘豹,下載第三方工具 iOSImagesExtractor
诺祸,下載地址https://github.com/devcxm/iOS-Images-Extractor
7.block的實質(zhì)定義
你需要區(qū)分是說block對象筷笨,還是block里面的代碼段胃夏。
block對象就是一個結(jié)構(gòu)體,里面有isa指針指向自己的類(global malloc stack)照雁,有desc結(jié)構(gòu)體描述block的信息答恶,__forwarding指向自己或堆上自己的地址悬嗓,如果block對象截獲變量,這些變量也會出現(xiàn)在block結(jié)構(gòu)體中曙求。最重要的block結(jié)構(gòu)體有一個函數(shù)指針悟狱,指向block代碼塊堰氓。block結(jié)構(gòu)體的構(gòu)造函數(shù)的參數(shù)双絮,包括函數(shù)指針得问,描述block的結(jié)構(gòu)體宫纬,自動截獲的變量(全局變量不用截獲)膏萧,引用到的__block變量榛泛。(__block對象也會轉(zhuǎn)變成結(jié)構(gòu)體)
block代碼塊在編譯的時候會生成一個函數(shù),函數(shù)第一個參數(shù)是前面說到的block對象結(jié)構(gòu)體指針孤个。執(zhí)行block齐鲤,相當于執(zhí)行block里面__forwarding里面的函數(shù)指針覆享。
我被面試的話撒顿,我會接下來和面試官討論一下block中內(nèi)存管理相關(guān)知識荚板。
用self調(diào)用帶有block的方法會引起循環(huán)引用跪另, 并不是所有通過self調(diào)用帶有block的方法會引起循環(huán)引用,需要看方法內(nèi)部有沒有持有self唧席。
? ? 使用weakSelf 結(jié)合strongSelf
的情況下淌哟,能夠避免循環(huán)引用辽故,也不會造成提前釋放導致block內(nèi)部代碼無效誊垢。
/*
防止block循環(huán)引用: __weak __typeof(self) weakSelf = self;
__strong typeof(weakSelf) strongSelf = weakSelf;
*/
8.點擊UITextView時鍵盤彈出擋住控件時的方法
//注冊鍵盤出現(xiàn)與隱藏時候的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboadWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
-
(void)keyboardWillShow:(NSNotification *)aNotification {
/* 獲取鍵盤的高度 */
NSDictionary *userInfo = aNotification.userInfo;
NSValue aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = aValue.CGRectValue;
/ 輸入框上移 */
CGRect frame = yijianView.frame;
CGFloat height = kHeight - frame.origin.y - frame.size.height;
if (height < keyboardRect.size.height) {[UIView animateWithDuration:0.5 animations:^ { CGRect frame = self.view.frame; frame.origin.y = -(keyboardRect.size.height - height ); self.view.frame = frame; }];
}
} -
(void)keyboardWillHide:(NSNotification *)aNotification {
/* 輸入框下移 */
[UIView animateWithDuration:0.5 animations:^ {
CGRect frame = self.view.frame;
frame.origin.y = kStatusBarHeight;
self.view.frame = frame;
}];
}
9.關(guān)于AutoLayoutd的講解http://www.tuicool.com/articles/AF3UFn2
//禁止自動轉(zhuǎn)換AutoresizingMask
btn2.translatesAutoresizingMaskIntoConstraints = NO;
//居中
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:btn2
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.45
constant:0]];
//距離底部20單位
//注意NSLayoutConstraint創(chuàng)建的constant是加在toItem參數(shù)的殃饿,所以需要-20。
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:btn2
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.65
constant:0]];
//定義高度是父View的三分之一
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.22 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.22 constant:0]];
10.給按鈕添加動畫
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 0.5;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[btn.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
11.滑動tableView cell重疊問題
重用機制調(diào)用的就是dequeueReusableCellWithIdentifier這個方法,方法的意思就是“出列可重用的cell”秒咐,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell)携取,就可以不使用重用機制,因而問題就可以得到解決不撑,但會浪費一些空間
http://blog.csdn.net/mhw19901119/article/details/9083293
12.cell以及l(fā)abel的自適應
cell.textLabel.textAlignment=0;
cell.textLabel.numberOfLines=0;
[cell.textLabel sizeToFit];
- 獲取網(wǎng)頁滑動高度焕檬,一般在網(wǎng)頁加載完給frame賦值webViewDidFinishLoad
self.webViewHeight = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
14.加載UIWebView或者WKWebView時報錯加下面的到info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
15.防止程序殺死的方法APPdelegate里添加
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSRunLoop currentRunLoop];
}
16.跳轉(zhuǎn)頁面隱藏底部tabbar在跳轉(zhuǎn)方法添加self.hidesBottomBarWhenPushed = YES;
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = YES;
}
17.分區(qū)呈現(xiàn)
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *tempArray = [self.arraycount[section]objectForKey:@"list"];
return tempArray.count;
}
18.在tableViewcell上監(jiān)聽cell上按鈕的indexpath(哪一行)進行一些處理
// cell上'edit按鈕'的點擊事件-
(IBAction)editClick:(id)sender {
// create toVC
AddressEditTableController toVC = [[AddressEditTableController alloc] initWithStyle:UITableViewStyleGrouped];
// 獲取'edit按鈕'所在的cell
(1)第一種
UITableViewCell myCell = (UITableViewCell *)[btn superview];
NSIndexPath *index = [self.tableview indexPathForCell:myCell];
(2)第二種
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
// 獲取cell的indexPath
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// 打印 --- test
NSLog(@"點擊的是第%zd行",indexPath.row + 1);// 跳轉(zhuǎn)
[self.navigationController pushViewController:toVC animated:YES];
}
19.點擊cell上的某個控件刪除這行cell
1). NSMutableArray *arr = self.arraycount[index.section];
[arr removeObjectAtIndex:index.row];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationFade)];
});
2). // 刪除數(shù)據(jù)源
[strongSelf.addressArray removeObjectAtIndex:indexPath.row];
// 主線程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.addressListTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationMiddle)];
20.//利用通知刪除第一響應方法
- (void)setUpForDismissKeyboard {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTapGR =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)];
NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
[nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){
[self.view addGestureRecognizer:singleTapGR];
}];
[nc addObserverForName:UIKeyboardWillHideNotification object:nil
queue:mainQuene usingBlock:^(NSNotification *note){
[self.view removeGestureRecognizer:singleTapGR];
}];
} - (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
[self.view endEditing:YES];
}
21.給父view設(shè)置透明度不讓子view受影響
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
22.獲取當前cell的index id
IFYDdthreeTableViewCell *cell = (IFYDdthreeTableViewCell *)[[btn superview]superview];
// 獲取cell的indexPath
NSIndexPath *index = [self.tableView indexPathForCell:cell];
NSString *st = [self.arrayID[index.section] objectForKey:@"id"] ;
23.根據(jù)返回值獲取文字寬度 不規(guī)則排序
//計算文字大小
CGSize titleSize = [_titleArr[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, titBtnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titBtn.titleLabel.font} context:nil].size;
CGFloat titBtnW = titleSize.width + 2 * padding;
//判斷按鈕是否超過屏幕的寬
if ((titBtnX + titBtnW) > kScreenW) {
titBtnX = 0;
titBtnY += titBtnH + padding;
}
//設(shè)置按鈕的位置
titBtn.frame = CGRectMake(titBtnX, titBtnY, titBtnW, titBtnH);
titBtnX += titBtnW + padding;
24.自動行高(需要配合autolayout自動布局)
self.tableView.estimatedRowHeight = 200; //預估行高self.tableView.rowHeight = UITableViewAutomaticDimension;
tableViewCell左滑功能
//tableView向左滑的功能
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{ self.tableView.editing = !self.tableView.editing; ChangeInfosController changeInfo = [[ChangeInfosController alloc]init]; [self.navigationController pushViewController:changeInfo animated:YES];}//修改左滑的文字-(NSString)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"編輯";}
取消重用
NSString*CellIdentifier = [NSStringstringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];//以indexPath來唯一確定cellFillOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[FillOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
25.UINavigationBar偏移量
我們知道默認translucent = YES腊敲。就是Bar 是有透明度的碰辅。
在有透明度的情況下介时,系統(tǒng)默認automaticallyAdjustsScrollViewInsets屬性是YES就是對于scrollerview的子類會默認content偏移64個單位沸柔。這樣做的目的,既然Bar都是透明的了羹蚣,系統(tǒng)就覺得你的scrollerView一定在會在(0乱凿,0)點,content偏移一個64個單位胁出。在你滑動的時候全蝶,隱藏在Bar下面的Content會有一個模糊顯示的效果。QAQ
如果你不想要這個偏移量
1 設(shè)置translucent = NO,既然Bar不透明绷落。自然不需要偏移量嘍
2 關(guān)閉這個偏移量砌烁,automaticallyAdjustsScrollViewInsets = NO
提一下像tableView催式,在storyboard 設(shè)置上下左右的約束荣月。結(jié)果cell上面多了一塊空白。就是這個問題捐下。
26.// 解決TabBar遮擋
self.edgesForExtendedLayout = UIRectEdgeAll;
self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(self.tabBarController.tabBar.frame), 0.0f);
27.uitextview設(shè)置占位符
遵循代理堂氯,label.enabled = NO咽白,實現(xiàn)方法
- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length == 0) {
}
}
28.pop到指定的頁面
IFYEShopViewController *homeVC = [[IFYEShopViewController alloc] init];
UIViewController *target = nil;
for (UIViewController * controller in self.navigationController.viewControllers) { //遍歷
if ([controller isKindOfClass:[homeVC class]]) { //這里判斷是否為你想要跳轉(zhuǎn)的頁面
target = controller;
}
}
if (target) {
[self.navigationController popToViewController:target animated:YES]; //跳轉(zhuǎn)
}
29.往數(shù)組里插入數(shù)組
[_arrayquanxuan insertObjects:_waijiaArr atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _waijiaArr.count)]];
- 字體變大變顏色
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%f",[yuyuejin floatValue] ]attributes:nil];
[attributedString setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.97 green:0.38 blue:0.13 alpha:1.0]} range:NSMakeRange(6, 2)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40] range:NSMakeRange(1, attributedString.length)];
jiage.text = [NSString stringWithFormat:@"約%@元", attributedString];
31.在請求數(shù)據(jù)時參數(shù)用下面的方法報錯時先檢查看看參數(shù)是否為nil晶框,如果為nil則停止授段,解決辦法為把為nil的參數(shù)在請求數(shù)據(jù)之前賦值
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"" :@"" , nil];
32.webview給后臺傳值傳參數(shù)時不能傳漢字如果有漢字 不會走代理方法
解決辦法把漢字轉(zhuǎn)utf8碼
http://jingjizhuli.tuowei.com/jingjizhuli/CaiZhengShouRuWanChengQuXianTu.aspx?DanWei=全部&Value=-1&Year=2017
33.UIimageView設(shè)置圓角不好使將多余的部分切掉
//將多余的部分切掉
// _image.layer.masksToBounds = YES;
34.連續(xù)發(fā)出幾個網(wǎng)絡(luò)請求侵贵,等這幾個請求完成才執(zhí)行
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 100; i++) {
dispatch_group_enter(group);
[[NetworkTool shareTool] post:url para:para block:^(id responseObject, NSError *error) {
dispatch_group_leave(group);
}
];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//請求完畢后的處理
});
- 請求數(shù)據(jù)時發(fā)現(xiàn)報錯但是數(shù)據(jù)還保存上了 只是某一字段沒保存上那就肯定是后臺的毛病啦
直接報錯-404 -500之類的是服務(wù)器出錯少字段
在報錯的位置打印一下就知道了
NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"服務(wù)器的錯誤原因:%@",str);
36.//json格式字符串轉(zhuǎn)字典:
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失斍嫌:%@",err);
return nil;
}
return dic;
}
37.禁止粘貼標點及符號
define NUM @"0123456789"
define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
-
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == mobilePhoneTextField) {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];}
return textField;
}