iOS开发小技巧总结

来源:互联网 发布:安卓休闲游戏 知乎 编辑:程序博客网 时间:2024/05/16 15:28

1、当使用UITableView 的Plain风格时,cell的数量占不满一屏时,会出现无用的cell分割线,如何去掉呢?

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
  2. {  
  3.     return 0.01f;  
  4. }  
  5. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section  
  6. {  
  7.     return [UIView new];  
  8.       
  9.     // If you are not using ARC:  
  10.     // return [[UIView new] autorelease];   
  11. }  

2、如何获取iOS 的idfa和mac地址

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.                     //for mac  
  2. #include <sys/socket.h>   
  3. #include <sys/sysctl.h>  
  4. #include <net/if.h>   
  5. #include <net/if_dl.h>  
  6.   
  7. //for idfa   
  8. #import <AdSupport/AdSupport.h>   
  9.   
  10. - (NSString * )macString{  
  11.       
  12.     int                 mib[6];  
  13.     size_t              len;  
  14.     char                *buf;  
  15.     unsigned char       *ptr;  
  16.     struct if_msghdr    *ifm;  
  17.     struct sockaddr_dl  *sdl;  
  18.       
  19.     mib[0] = CTL_NET;  
  20.     mib[1] = AF_ROUTE;  
  21.     mib[2] = 0;  
  22.     mib[3] = AF_LINK;  
  23.     mib[4] = NET_RT_IFLIST;  
  24.       
  25.     if ((mib[5] = if_nametoindex("en0")) == 0) {  
  26.         printf("Error: if_nametoindex error\n");  
  27.         return NULL;  
  28.     }  
  29.       
  30.     if (sysctl(mib, 6NULL, &len, NULL0) < 0) {  
  31.         printf("Error: sysctl, take 1\n");  
  32.         return NULL;  
  33.     }  
  34.       
  35.     if ((buf = malloc(len)) == NULL) {  
  36.         printf("Could not allocate memory. error!\n");  
  37.         return NULL;  
  38.     }  
  39.       
  40.     if (sysctl(mib, 6, buf, &len, NULL0) < 0) {  
  41.         printf("Error: sysctl, take 2");  
  42.         free(buf);  
  43.         return NULL;  
  44.     }  
  45.       
  46.     ifm = (struct if_msghdr *)buf;  
  47.     sdl = (struct sockaddr_dl *)(ifm + 1);  
  48.     ptr = (unsigned charchar *)LLADDR(sdl);  
  49.     NSString *macString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",  
  50.                            *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];  
  51.     free(buf);  
  52.       
  53.     return macString;  
  54. }  
  55.   
  56. - (NSString *)idfaString {  
  57.       
  58.     NSBundle *adSupportBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AdSupport.framework"];  
  59.     [adSupportBundle load];  
  60.       
  61.     if (adSupportBundle == nil) {  
  62.         return @"";  
  63.     }  
  64.     else{  
  65.           
  66.         Class asIdentifierMClass = NSClassFromString(@"ASIdentifierManager");  
  67.           
  68.         if(asIdentifierMClass == nil){  
  69.             return @"";  
  70.         }  
  71.         else{  
  72.               
  73.             //for no arc   
  74.             //ASIdentifierManager *asIM = [[[asIdentifierMClass alloc] init] autorelease];  
  75.             //for arc   
  76.             ASIdentifierManager *asIM = [[asIdentifierMClass alloc] init];  
  77.               
  78.             if (asIM == nil) {  
  79.                 return @"";  
  80.             }  
  81.             else{  
  82.                   
  83.                 if(asIM.advertisingTrackingEnabled){  
  84.                     return [asIM.advertisingIdentifier UUIDString];  
  85.                 }  
  86.                 else{  
  87.                     return [asIM.advertisingIdentifier UUIDString];  
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92. }  
  93.   
  94. - (NSString *)idfvString  
  95. {  
  96.     if([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {  
  97.         return [[UIDevice currentDevice].identifierForVendor UUIDString];  
  98.     }  
  99.       
  100.     return @"";  
  101. }  
  102.                   
不过请注意:iOS7之后,mac地址就获取不到了。参考(转):

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 英文原文:In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02:00:00:00:00:00. If you need to identify the device, use the identifierForVendor property of UIDevice instead. (Apps that need an identifier for their own advertising purposes should consider using the advertisingIdentifier property of ASIdentifierManager instead.)  
  2. 翻译:从iOS7及更高版本往后,如果你向ios设备请求获取mac地址,系统将返回一个固定值“02:00:00:00:00:00”,如果你需要识别设备的 唯一性,请使用UIDevice的identifierForVendor属性。(因广告目的而需要识别设备的应用,请考虑使用 ASIdentifierManager的advertisingIdentifier属性作为替代)  

3、是不是为了UITextFiled在TableView中被遮挡而烦恼,试试下面的这段代码把。  让TableViewCell中UITextFiled随点击滚动到可视位置

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //textfile uitableview滚动   
  2.   
  3.   
  4.     UITableViewCell *cell;  
  5.     if (!IS_OS_7_OR_LATER) {  
  6.         // Load resources for iOS 6.1 or earlier  
  7.         cell = (UITableViewCell *) textField.superview.superview;  
  8.           
  9.     } else {  
  10.         // Load resources for iOS 7 or later  
  11.         cell = (UITableViewCell *) textField.superview.superview.superview;  
  12.         // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Cell!  
  13.     }  

4、让UITableView的Cell不重用

有时候我们的UITableview的cell是有限的10个8个的,根本没必要重用。重用反而导致很多问题。其中思路就是,给这有限的10个cell不同的标示

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  2. {   
  3.        
  4.     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell   
  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell   
  6.     if (cell == nil) {   
  7.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];   
  8.     }   
  9. }   

5、在iOS 7,如何检测到系统自带ViewController手势返回结束

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated  
  2. {  
  3.         id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;  
  4.         [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {  
  5.             NSLog(@"7: %i", [context isCancelled]);  
  6.     }];  
  7. }  

这个检测需要设置 self.navigationController.delegate =self; 当前viewController要UINavigationBarDelegate实现此协议。

还有一个关键的设置,需要在当前ViewController适当的地方设置self.navigationController.delegate = nil;  否则会导致崩溃。我是这样设置的,

在viewDidAppear设置self.navigationController.delegate = self; viewDidDisAppear时设置self.navigationController.delegate = nil;

保证设置成双成对。

参考:http://stackoverflow.com/questions/20639006/getting-interactivepopgesturerecognizer-dismiss-callback-event


6、如何设置Plain 风格下UITableView的Section的HeaderView不在UITableview上浮动

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. CGFloat dummyViewHeight = 40;  
  2. UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(00self.tableView.bounds.size.width, dummyViewHeight)];  
  3. self.tableView.tableHeaderView = dummyView;  
  4. self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 000);  

上面dummyViewHeight的值根据自己headerView的高度变化。就是你headerView的高度。


7、解决企业证书在7.1系统下,无法安装 证书报错的问题

思路:服务器证书或自签名的证书不行,DropBox的网盘的文件访问是https的。可以解决这个问题可以把plist文件传输到DropBox网盘里,访问即可。

把manifest的plist文件放到Dropbox中,并拷贝出分享链接,如:https://www.dropbox.com/s/sdljgdlsj24343j.plist

至于manifest的ipa包的文件可以放到自己的服务器上。这样访问速度和上传大小都不收限制。这个方法我试验过,非常好用。

0 0
原创粉丝点击