ios 写项目的时候遇到的问题及解决方案
1.解决headerView不随cell一起滚动的问题
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.newsTV reloadData];
});
}];
self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// 进入刷新状态后会自动调用这个block
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.newsTV reloadData];
});
}];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ;
}
8.强制旋转问题
问题解决了, 还算完美. 方法基本道理还是一样, 强制调用系统的自动旋转机制.
竖屏点击按钮 旋转到横屏
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//这句话是防止手动先把设备置为横屏,导致下面的语句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
横屏点击按钮, 旋转到竖屏
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//这句话是防止手动先把设备置为竖屏,导致下面的语句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
同时还要必须支持自动旋转
- (BOOL)shouldAutorotate
{
return YES;
}
然后就是
- (NSUInteger)supportedInterfaceOrientations
{
if (IS_IPHONE && self.isHalfScreen) { //如果是iPhone,且为竖屏的时候, 只支持竖屏
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskLandscape; //否者只支持横屏
}
这里基本就可以了.
获取方法:
UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
首先 [[NSDate date] timeIntervalSince1970] 是可以获取到后面的毫秒 微秒的 ,只是在保存的时候省略掉了, 如一个时间戳不省略的情况下为 1395399556.862046 ,省略掉后为一般所见 1395399556 。所以想取得毫秒时用获取到的时间戳 *1000 ,想取得微秒时 用取到的时间戳 * 1000 * 1000 。
如果你想格式化输出 可以:
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];
NSString *date = [formatter stringFromDate:[NSDate date]];
NSString *timeLocal = [[NSString alloc] initWithFormat:@"%@", date];
NSLog(@"%@", timeLocal);
11.自适应文本高度
1 NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14]}; 2 3 CGRect rect = [text boundingRectWithSize:CGSizeMake(ViewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; 4 5 return rect.size.height;
12 iOS9 适配设置
实际上在Xcode 7中,我们新建一个iOS程序时,bitcode选项默认是设置为YES的。我们可以在”Build Settings”->”Enable Bitcode”选项中看到这个设置。
要么让第三方库支持,要么关闭target的bitcode选项。
13. iOS9 HTTPs转HTTP
1 <key>NSAppTransportSecurity</key> 2 3 <dict> 4 5 <!--Connect to anything (this is probably BAD)--> 6 7 <key>NSAllowsArbitraryLoads</key> 8 9 <true/> 10 11 </dict>
14.截取字符串
[str substringFromIndex:6];
substringWithRange:NSMakeRange(4,2)截取字符串的一部分,从第4位开始,截取两位
substringToIndex: n截取到第几位
(substringFromIndex:n)字符串从第n 位开始截取,直到最后
16.NSScanner: nil string argument
错误原因是我们在调用某个方法的时候,传入了一个空字符串(注意区别于字符串内容为空)作为方法参数。
我从服务器上获取某字符串数据,考虑到有些对象不含这个字符串变量,我在使用时先判断该字符串是否为空,例如:
假设,这个字符串名叫str,
先判断if(str!=nil){
//do something
double data=[str doubleValue];
}
但是,当数据为空时依旧报错,
苹果官方文档时,有这么一个代码:
- id aValue = [arrayWithNull objectAtIndex:0];
- if (aValue == nil) {
- NSLog(@"equals nil");
- } else if (aValue == [NSNull null]) {
- NSLog(@"equals NSNull instance");
- if ([aValue isEqual:nil]) {
- NSLog(@"isEqual:nil");
- }
- }
- // output: "equals NSNull instance”
虽然最后我的问题解决了,我在if判断中用
- if(![tmpNewsModel.latitude isEqual:[NSNull null]]){
- //do something
- }
问题是解决了,但是还不太理解nil和NSNull的区别?
17.iphone 尺寸
18.修改mac host文件
sudo nano /etc/hosts
同样是输入密码,打开 hosts 文件,根据你的需要对该文件进行编辑,编辑完毕之后按 ctrl+o 保存,
出现 File Name to Write: /etc/hosts 的时候按回车确认,再按 ctrl+x 退出即可。
19. IOS8 设置TableView Separatorinset 分割线从边框顶端开始
经过测试加入下面方法 在ios7 8上都可以正常工作
1 -(void)viewDidLayoutSubviews 2 3 { 4 5 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { 6 7 [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)]; 8 9 } 10 11 12 13 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { 14 15 [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; 16 17 } 18 19 } 20 21 22 23 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 24 25 { 26 27 if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { 28 29 [cell setSeparatorInset:UIEdgeInsetsZero]; 30 31 } 32 33 34 35 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { 36 37 [cell setLayoutMargins:UIEdgeInsetsZero]; 38 39 } 40 41 }
20.url 里面不能有中文字符,需转换否会请求出错
22.看了苹果的文档,里面有这一句话:All launch images must be PNG files and must reside in the top level of your application’s bundle directory
来是不能用jpg做启动画面的
23 UIWebView打开太耗内存的处理方法,
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}
APPdelegate里面 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
} -
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
int cacheSizeMemory = 410241024; // 4MB
int cacheSizeDisk = 3210241024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];return YES;
}
24 iOS9以后简单的UIAlertView已经不能用了
UIAlertController alert=[UIAlertController alertControllerWithTitle:msg message:detail preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction okAction=[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
- #pragma mark 处理键盘消失
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.view endEditing:YES];
}
26.问题:求10000以内的质数
质数:只能被1和它本身整除的数.即不能整除除1以外比它小的任何数
算法:
include
void main()
{
int a,b;
for(a=1;a<=10000;a++)
{
for(b=2;b<a;b++)
{
if(a%b==0) break;
}
if (a==b) printf("%d ",a);
}
}
27.问题: ios9 适配运行项目打出下面日志
-canOpenURL: failed for URL: "weixin://app/wxdb5f20e4f759e04f/" - error: "(null)"
原因:要用iOS9的真机!不要用模拟器测试,不然一直报-canOpenURL: failed for URL: "XXXXXXXXXX" - error: "(null)”.
真机测试可以通过
28: UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
1,UIView的setNeedsDisplay和setNeedsLayout方法
首先两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到 UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用layoutSubViews,
就可以 处理子视图中的一些数据。
综上所诉,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。
layoutSubviews在以下情况下会被调用:
1、init初始化不会触发layoutSubviews。
2、addSubview会触发layoutSubviews。
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
4、滚动一个UIScrollView会触发layoutSubviews。
5、旋转Screen会触发父UIView上的layoutSubviews事件。
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
7、直接调用setLayoutSubviews。
drawRect在以下情况下会被调用:
1、如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。drawRect调用是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).
2、该方法在调用sizeToFit后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3、通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4、直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0。
以上1,2推荐;而3,4不提倡
drawRect方法使用注意点:
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似于drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕