zoukankan      html  css  js  c++  java
  • iOS开发中的小Tips

    http://shavekevin.com/2016/04/12/iosdeveloptips/

    1.取消cell的分割线

    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;  
    

    2.UITabelviewCell 的高亮状态的取消
    用以下方法:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        //设置cell的背景是透明的。
        cell.backgroundColor = [UIColor clearColor];
        //取消cell的高亮状态
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    //使用下面的这个方法会导致cell不能响应点击事件
    
    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    

    3.设置分割线的左右偏移量

    tableview  setSeparatorInset:inset  
    

    4.类的声明和实现,书写的时候。容易犯的错误有一下几种情况。

    (1).只写声明,不写实现。

    (2).将@end这个结束标记忘记了。

    (3).类的声明或者实现都不能写下C语言的函数中。

    (4).属性的声明必须写在大括号中。

    (5).在声明属性的时候,不能够直接赋值。

    (6).声明和实现不能够嵌套使用。

    5.更换头像后,好友不会立马看到我们更换的新头像。请问使用来哪种机制来更新本地的缓存。

    IM透传 发个消息过去 携带新头像 好友接到后更新。

    6.推送如何跳转到对应的controller

    跳转界面

    - (void)push:(NSDictionary *)params
    {
        // 类名
        NSString *class =[NSString stringWithFormat:@"%@", params[@"class"]];
        const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];
    
        // 从一个字串返回一个类
        Class newClass = objc_getClass(className);
        if (!newClass)
        {
            // 创建一个类
            Class superClass = [NSObject class];
            newClass = objc_allocateClassPair(superClass, className, 0);
            // 注册你创建的这个类
            objc_registerClassPair(newClass);
        }
        // 创建对象
        id instance = [[newClass alloc] init];
     // 对该对象赋值属性
        NSDictionary * propertys = params[@"property"];
        [propertys enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            // 检测这个对象是否存在该属性
            if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
                // 利用kvc赋值
                [instance setValue:obj forKey:key];
            }
        }];
    
        // 获取导航控制器
        UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
        UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];
        // 跳转到对应的控制器
        [pushClassStance pushViewController:instance animated:YES];
    }
    

    检测对象是否存在该属性

    - (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName
    {
        unsigned int outCount, i;
    
        // 获取对象里的属性列表
        objc_property_t * properties = class_copyPropertyList([instance
                                                               class], &outCount);
    
        for (i = 0; i < outCount; i++) {
            objc_property_t property =properties[i];
            //  属性名转成字符串
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            // 判断该属性是否存在
            if ([propertyName isEqualToString:verifyPropertyName]) {
                free(properties);
                return YES;
            }
        }
        free(properties);
    
        return NO;
    }
    

    来源于简书: iOS 万能跳转界面方法 runtime实用篇一

    7.3D效果 还不错哦

    https://github.com/nicklockwood/iCarousel

    8.xcode7引入xmpp提示module libxmlsimu not found怎么解决?

    解决方案如下图:

    9.data进行MD5 加密

    https://github.com/shaojiankui/iOS-Categories/blob/master/Categories/Foundation/NSData/NSData%2BHash.m

    10.UIVisualEffectView 背景是虚化的(类似我们iphone查看通知时的虚化背景)

    https://github.com/nicklockwood/FXBlurView

    11.NSDate 的坑

    Also when using a date format string using the correct format is important.

    @"YYYY" is week-based calendar year.

    @"yyyy" is ordinary calendar year.

    可以看下面这篇博客:NSDateFormatter 'YYYY' 和 'yyyy' 的区别

    12.swift 实时滤镜

    http://blog.csdn.net/zhangao0086/article/details/39433519

    13.动态加载视频

    http://www.jianshu.com/p/3dcebf0493d1

    14.swift 闭包

    http://www.henishuo.com/closures-of-swift/

    15.理解contentsScale

    http://joeshang.github.io/2015/01/10/understand-contentsscale/#disqus_thread

    16.图文混排

    https://github.com/12207480/TYAttributedLabel

    17.取消所有请求

    [NSObject cancelPreviousPerformRequestsWithTarget:selfselector:@selector(sendContentReqData)object:nil];
    

    18.刷新tableView某一行

    [m_tableView reloadRowsAtIndexPaths:[NSArrayarrayWithObject:[NSIndexPathindexPathForRow:m_selectRowinSection:0]]withRowAnimation:UITableViewRowAnimationRight];
    

    19.textField 文字上下居中

    textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
    
    
    //.全部删除按钮
    
    textField.clearButtonMode =UITextFieldViewModeWhileEditing;
    
    [textFieldsetAutocapitalizationType:UITextAutocapitalizationTypeNone];
    

    20.模拟导航条颜色

     UINavigationBar *tmpNavBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44)];
    
    tmpNavBar.barStyle=UIBarStyleDefault;
    
     self.m_navigationBar=tmpNavBar;
     UINavigationItem *tmpNavItem=[[UINavigationItemalloc]initWithTitle:@"Search"];
     UIBarButtonItem *tmpBarItem=[[UIBarButtonItem alloc]initWithTitle:@"Done"style:UIBarButtonItemStyleDone target:selfaction:@selector(exitSearchScreen)];
    
      [tmpNavItem setRightBarButtonItem:tmpBarItem];
    
     [m_navigationBar setItems:[NSArray arrayWithObject:tmpNavItem]];
    
      [self addSubview:m_navigationBar];
    

    21.数组、字典内容不能为空,装的是地址,为空就是野指针

    22.数组,字典访问不能越界

    23.代理最好在dealloc里设置为nil,单例必须设置,因为一个类释放了,但是单例指针还指向这个类,可能会出问题。

    24.防止野指针,代理处崩溃多为此现象。

    25.字符串判断不为空,一般 str!= nil && ![str isEqualTo@""] , 不等于nil是有地址,后者是内容不为空。

    26.下面的view或self.view如果小于上面的,上面的view超过下面的部分将无法与用户交互。

    27.自定义cell时,最好在cell.contentView上加控件

    28.尽量使用点语法,减少内存泄漏

    29.正则

    (?<=srcs*=s*")[dD]+?(?=")
    

    取出html 中的src 图片

    30.不能重复释放

    31.两个对象不能相互引用

    32.dealloc的super dealloc必须放在最后面

    33.所有不带星号的和id类型的都只能assign

    34.系统线程也有副线程

    35.一些变量最好都初始化,且写在一个方法里,需要恢复开始的状态时可以直接调用该方法恢复

    36.初始化的指针都要赋值为nil,系统不会帮你这么做的

    37.block 对象语法

    (1).在内联Block Objects可以直接读取对象self,独立的BlockObjects若想读取self对象必须将其设置为参数传递进去。

    (2).在内联Block Objects可以直接用点语法读取对象self的属性,也可以使用setter和 getter方法,但是,独立的Block Objects只能用setter and getter方法读取对象self的属性

    38.改变导航条颜色

    [nav.navigationBar setTintColor:[UIColorgrayColor]];
    
    nav.navigationBar.barStyle =UIBarStyleBlackTranslucent;  
    

    39.程序崩溃处理方法

    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    
    {
    
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
    
    }
    

    调用下面的方法,如数组越界,调用不存在的方法等会打出详细的信息。

    void UncaughtExceptionHandler(NSException*exception) {
    
       NSArray *arr = [exception callStackSymbols];
    
       NSString *reason = [exception reason];
    
       NSString *name = [exception name];
    
       NSLog(@"
    name: %@
    eason: %@
    userInfo: %@
    callStackSymbols:%@
    callStackReturnAddresses: %@",name,reason,[exceptionuserInfo],arr,[exceptioncallStackReturnAddresses]);
    
    }
    

    开启僵尸模式 Object-C的Enable Zombie Objects勾选,Memory 中的MallocStack勾选,Exceptions里的Log Exceptions勾选

    40.NSLog 怎么打印出变量类型的?

    NSStringFromSelector(SEL aSelector);
    
    NSSelectorFromString(NSString *aSelectorName);
    
    NSStringFromClass(Class aClass);
    
    NSClassFromString(NSString *aClassName);
    
    NSStringFromProtocol(Protocol *proto)
    
    NSProtocolFromString(NSString *namestr) 
    
    NSStringFromCGPoint(CGPoint point);
    
    NSStringFromCGVector(CGVector vector);
    
    NSStringFromCGSize(CGSize size);
    
    NSStringFromCGRect(CGRect rect);
    
    NSStringFromCGAffineTransform(CGAffineTransform transform);
    
    NSStringFromUIEdgeInsets(UIEdgeInsets insets);
    
    NSStringFromUIOffset(UIOffset offset);
    
    CGPointFromString(NSString *string);
    
    CGVectorFromString(NSString *string);
    
    CGSizeFromString(NSString *string);
    
    CGRectFromString(NSString *string);
    
    CGAffineTransformFromString(NSString *string);
    
    UIEdgeInsetsFromString(NSString *string);
    
    UIOffsetFromString(NSString *string);  
    
  • 相关阅读:
    数据库主体拥有数据库角色,无法删除
    document.documentElement.scrollTop Google Chrome 不 兼容
    js 获取 焦点的元素 兼容 ie 火狐 safari 等
    LNMP分离安装
    WEB开发模式调整
    牛出租车司机(转)
    nginx优化(转)
    pam_mysql编译安装(Ubuntu10.10)
    CruiseControl.NET使用
    nginx学习资料收集(补充中....)
  • 原文地址:https://www.cnblogs.com/superbobo/p/5392088.html
Copyright © 2011-2022 走看看