好久沒寫部落格了,現在總結一下平時開發時遇到的一些問題,以及解決方案。下面以問答方式來記錄
1、當使用UITableView 的Plain風格時,cell的數量占不滿一屏時,會出現無用的cell分割線,如何去掉呢?
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01f;}- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ return [UIView new]; // If you are not using ARC: // return [[UIView new] autorelease];}
2、如何擷取iOS 的idfa和mac地址
//for mac#include <sys/socket.h>#include <sys/sysctl.h>#include <net/if.h>#include <net/if_dl.h>//for idfa#import <AdSupport/AdSupport.h>- (NSString * )macString{ int mib[6];size_t len;char *buf;unsigned char *ptr;struct if_msghdr *ifm;struct sockaddr_dl *sdl; mib[0] = CTL_NET;mib[1] = AF_ROUTE;mib[2] = 0;mib[3] = AF_LINK;mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex("en0")) == 0) {printf("Error: if_nametoindex error\n");return NULL;} if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {printf("Error: sysctl, take 1\n");return NULL;} if ((buf = malloc(len)) == NULL) {printf("Could not allocate memory. error!\n");return NULL;} if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {printf("Error: sysctl, take 2");free(buf);return NULL;} ifm = (struct if_msghdr *)buf;sdl = (struct sockaddr_dl *)(ifm + 1);ptr = (unsigned char *)LLADDR(sdl);NSString *macString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];free(buf); return macString;}- (NSString *)idfaString { NSBundle *adSupportBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AdSupport.framework"]; [adSupportBundle load]; if (adSupportBundle == nil) { return @""; } else{ Class asIdentifierMClass = NSClassFromString(@"ASIdentifierManager"); if(asIdentifierMClass == nil){ return @""; } else{ //for no arc //ASIdentifierManager *asIM = [[[asIdentifierMClass alloc] init] autorelease]; //for arc ASIdentifierManager *asIM = [[asIdentifierMClass alloc] init]; if (asIM == nil) { return @""; } else{ if(asIM.advertisingTrackingEnabled){ return [asIM.advertisingIdentifier UUIDString]; } else{ return [asIM.advertisingIdentifier UUIDString]; } } } }}- (NSString *)idfvString{ if([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) { return [[UIDevice currentDevice].identifierForVendor UUIDString]; } return @"";}不過請注意:iOS7之後,mac地址就擷取不到了。參考(轉):
英文原文: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.)翻譯:從iOS7及更高版本往後,如果你向ios裝置請求擷取mac地址,系統將返回一個固定值“02:00:00:00:00:00”,如果你需要識別裝置的 唯一性,請使用UIDevice的identifierForVendor屬性。(因廣告目的而需要識別裝置的應用,請考慮使用 ASIdentifierManager的advertisingIdentifier屬性作為替代)
3、是不是為了UITextFiled在TableView中被遮擋而煩惱,試試下面的這段代碼把。 讓TableViewCell中UITextFiled隨點擊滾動到可視位置
//textfile uitableview滾動 UITableViewCell *cell; if (!IS_OS_7_OR_LATER) { // Load resources for iOS 6.1 or earlier cell = (UITableViewCell *) textField.superview.superview; } else { // Load resources for iOS 7 or later cell = (UITableViewCell *) textField.superview.superview.superview; // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Cell! }
4、讓UITableView的Cell不重用
有時候我們的UITableview的cell是有限的10個8個的,根本沒必要重用。重用反而導致很多問題。其中思路就是,給這有限的10個cell不同的標示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } }
5、在iOS 7,如何檢測到系統內建ViewController手勢返回結束
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator; [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) { NSLog(@"7: %i", [context isCancelled]); }];}
這個檢測需要設定 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上浮動
CGFloat dummyViewHeight = 40; UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)]; self.tableView.tableHeaderView = dummyView; self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);
上面dummyViewHeight的值根據自己headerView的高度變化。就是你headerView的高度。
7、解決企業認證在7.1系統下,無法安裝 認證報錯的問題
思路:伺服器憑證或自簽名的認證不行,DropBox的網盤的檔案訪問是https的。可以解決這個問題可以把plist檔案傳輸到DropBox網盤裡,訪問即可。
把manifest的plist檔案放到Dropbox中,並拷貝出分享連結,如:https://www.dropbox.com/s/sdljgdlsj24343j.plist
至於manifest的ipa包的檔案可以放到自己的伺服器上。這樣訪問速度和上傳大小都不收限制。這個方法我實驗過,非常好用。
據說國內https://portal.qiniu.com/signin 七牛儲存也有這樣的功能。