工作筆記

1.按鈕多選循環(huán)字典通過(guò)tag判斷
點(diǎn)擊下一步時(shí)字典記得初始化
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)出來(lái)并且用逗號(hào)隔開(kāi)
NSString *string = [array componentsJoinedByString:@","]
取出字符串下標(biāo)前后的字符串
[string substringToIndex:7];
substringFromIndex
//截取下標(biāo)6后一位的字符串
[sr substringWithRange:NSMakeRange(6, 1)]

3.self.automaticallyAdjustsScrollViewInsets = NO;防止tableView的cell上移下移

4.每次進(jìn)入頁(yè)面刷新數(shù)據(jù)流程
[_arrayCount removeAllObjects];
[self request];

5.tableViewcell位置竄動(dòng)
當(dāng) tableView 的內(nèi)容比較多時(shí)底部的內(nèi)容反而顯示不下。這就很奇怪了,按照前面的結(jié)論,這時(shí)候 tableView是從導(dǎo)航欄底部開(kāi)始布局的,contentInset 也是(0,0,0,0),怎么底部的內(nèi)容會(huì)被遮擋一部分呢?原因在于self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
初始化時(shí) rootView 的 frame 還是(0,0,screenWidth,screenHeight),只需要在viewWillLayoutSubviews
中重新修改一下 tableview 的 frame 即可,

  • (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; _tableView.frame = self.view.bounds;
    }

6.提取App的UI素材
打開(kāi)iTunes,在App Store下載覺(jué)得UI不錯(cuò)的App,下載完成以后可以在我的應(yīng)用中看到App。將App直接拖拽到桌面,得到App的ipa文件,下載第三方工具 iOSImagesExtractor
,下載地址https://github.com/devcxm/iOS-Images-Extractor

7.block的實(shí)質(zhì)定義
你需要區(qū)分是說(shuō)block對(duì)象,還是block里面的代碼段。
block對(duì)象就是一個(gè)結(jié)構(gòu)體,里面有isa指針指向自己的類(global malloc stack),有desc結(jié)構(gòu)體描述block的信息,__forwarding指向自己或堆上自己的地址,如果block對(duì)象截獲變量,這些變量也會(huì)出現(xiàn)在block結(jié)構(gòu)體中。最重要的block結(jié)構(gòu)體有一個(gè)函數(shù)指針,指向block代碼塊。block結(jié)構(gòu)體的構(gòu)造函數(shù)的參數(shù),包括函數(shù)指針,描述block的結(jié)構(gòu)體,自動(dòng)截獲的變量(全局變量不用截獲),引用到的__block變量。(__block對(duì)象也會(huì)轉(zhuǎn)變成結(jié)構(gòu)體)
block代碼塊在編譯的時(shí)候會(huì)生成一個(gè)函數(shù),函數(shù)第一個(gè)參數(shù)是前面說(shuō)到的block對(duì)象結(jié)構(gòu)體指針。執(zhí)行block,相當(dāng)于執(zhí)行block里面__forwarding里面的函數(shù)指針。
我被面試的話,我會(huì)接下來(lái)和面試官討論一下block中內(nèi)存管理相關(guān)知識(shí)。
用self調(diào)用帶有block的方法會(huì)引起循環(huán)引用, 并不是所有通過(guò)self調(diào)用帶有block的方法會(huì)引起循環(huán)引用,需要看方法內(nèi)部有沒(méi)有持有self。
? ? 使用weakSelf 結(jié)合strongSelf
的情況下,能夠避免循環(huán)引用,也不會(huì)造成提前釋放導(dǎo)致block內(nèi)部代碼無(wú)效。
/*
防止block循環(huán)引用: __weak __typeof(self) weakSelf = self;
__strong typeof(weakSelf) strongSelf = weakSelf;
*/

8.點(diǎn)擊UITextView時(shí)鍵盤彈出擋住控件時(shí)的方法
//注冊(cè)鍵盤出現(xiàn)與隱藏時(shí)候的通知
[[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
    //禁止自動(dòng)轉(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.給按鈕添加動(dòng)畫(huà)
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.滑動(dòng)tableView cell重疊問(wèn)題
重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個(gè)方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell),就可以不使用重用機(jī)制,因而問(wèn)題就可以得到解決,但會(huì)浪費(fèi)一些空間
http://blog.csdn.net/mhw19901119/article/details/9083293

12.cell以及l(fā)abel的自適應(yīng)
cell.textLabel.textAlignment=0;
cell.textLabel.numberOfLines=0;
[cell.textLabel sizeToFit];

  1. 獲取網(wǎng)頁(yè)滑動(dòng)高度,一般在網(wǎng)頁(yè)加載完給frame賦值webViewDidFinishLoad

self.webViewHeight = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];

14.加載UIWebView或者WKWebView時(shí)報(bào)錯(cuò)加下面的到info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

15.防止程序殺死的方法APPdelegate里添加

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    [NSRunLoop currentRunLoop];
    }

16.跳轉(zhuǎn)頁(yè)面隱藏底部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)聽(tīng)cell上按鈕的indexpath(哪一行)進(jìn)行一些處理
    // cell上'edit按鈕'的點(diǎn)擊事件

  • (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(@"點(diǎn)擊的是第%zd行",indexPath.row + 1);

    // 跳轉(zhuǎn)
    [self.navigationController pushViewController:toVC animated:YES];
    }

19.點(diǎn)擊cell上的某個(gè)控件刪除這行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.//利用通知?jiǎng)h除第一響應(yīng)方法

  • (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.獲取當(dāng)前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ī)則排序
//計(jì)算文字大小
CGSize titleSize = [_titleArr[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, titBtnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titBtn.titleLabel.font} context:nil].size;
CGFloat titBtnW = titleSize.width + 2 * padding;

    //判斷按鈕是否超過(guò)屏幕的寬
    if ((titBtnX + titBtnW) > kScreenW) {
        titBtnX = 0;
        titBtnY += titBtnH + padding;
    }
    //設(shè)置按鈕的位置
    titBtn.frame = CGRectMake(titBtnX, titBtnY, titBtnW, titBtnH);
    
    titBtnX += titBtnW + padding;

24.自動(dòng)行高(需要配合autolayout自動(dòng)布局)
self.tableView.estimatedRowHeight = 200; //預(yù)估行高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來(lái)唯一確定cellFillOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[FillOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

25.UINavigationBar偏移量
我們知道默認(rèn)translucent = YES。就是Bar 是有透明度的。
在有透明度的情況下,系統(tǒng)默認(rèn)automaticallyAdjustsScrollViewInsets屬性是YES就是對(duì)于scrollerview的子類會(huì)默認(rèn)content偏移64個(gè)單位。這樣做的目的,既然Bar都是透明的了,系統(tǒng)就覺(jué)得你的scrollerView一定在會(huì)在(0,0)點(diǎn),content偏移一個(gè)64個(gè)單位。在你滑動(dòng)的時(shí)候,隱藏在Bar下面的Content會(huì)有一個(gè)模糊顯示的效果。QAQ
如果你不想要這個(gè)偏移量
1 設(shè)置translucent = NO,既然Bar不透明。自然不需要偏移量嘍
2 關(guān)閉這個(gè)偏移量,automaticallyAdjustsScrollViewInsets = NO
提一下像tableView,在storyboard 設(shè)置上下左右的約束。結(jié)果cell上面多了一塊空白。就是這個(gè)問(wèn)題。

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,實(shí)現(xiàn)方法

  • (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
    }
    }

28.pop到指定的頁(yè)面
IFYEShopViewController *homeVC = [[IFYEShopViewController alloc] init];
UIViewController *target = nil;
for (UIViewController * controller in self.navigationController.viewControllers) { //遍歷
if ([controller isKindOfClass:[homeVC class]]) { //這里判斷是否為你想要跳轉(zhuǎn)的頁(yè)面
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)]];

  1. 字體變大變顏色
    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.在請(qǐng)求數(shù)據(jù)時(shí)參數(shù)用下面的方法報(bào)錯(cuò)時(shí)先檢查看看參數(shù)是否為nil,如果為nil則停止,解決辦法為把為nil的參數(shù)在請(qǐng)求數(shù)據(jù)之前賦值
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"" :@"" , nil];

32.webview給后臺(tái)傳值傳參數(shù)時(shí)不能傳漢字如果有漢字 不會(huì)走代理方法
解決辦法把漢字轉(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ā)出幾個(gè)網(wǎng)絡(luò)請(qǐng)求,等這幾個(gè)請(qǐng)求完成才執(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(), ^{
        
        //請(qǐng)求完畢后的處理
        
    });
  1. 請(qǐng)求數(shù)據(jù)時(shí)發(fā)現(xiàn)報(bào)錯(cuò)但是數(shù)據(jù)還保存上了 只是某一字段沒(méi)保存上那就肯定是后臺(tái)的毛病啦
    直接報(bào)錯(cuò)-404 -500之類的是服務(wù)器出錯(cuò)少字段
    在報(bào)錯(cuò)的位置打印一下就知道了
    NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
    NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"服務(wù)器的錯(cuò)誤原因:%@",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.禁止粘貼標(biāo)點(diǎn)及符號(hào)

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;
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容