ipad 开发常用问题

1.设置tag方便使用 
在cell中放入多个控件的时候使用tag很方便的区分。在其他的地方使用tag也方便编程。呵呵 

2.今天试验一个关于删除的问题,也就是直接在cell中添加一个按钮,点击按钮实现删除操作,这个应该是Apple不允许的,没有设置到编辑模式,我使用的是直接reload数据源的方法,但是发现一个问题,也就是在选中该行的时候我想保存该行的行数,但是我保存不了,也不知道是为什么,难道是delegate的缘故,还是第一次点该行再次点的时候呢个值已经变了,但是我看的时候是空,郁闷。最后我是用NSMutableArray来实现的,在选中该行的时候我让NSMutableArray对象addObject该行然后在使用的时候取第一个值也就是NSMUtableArray ObjectAtIndex:0呵呵具体成果了。原理还在摸索中。 

3.UITableViewCell的重用机制 
UITableViewCell在加载的时候只更新内容,也就是说如果你在加载cell的时候在cell中加入button假如100个cell,你设置tag的时候是更加row加入,你想的到得tag是从0到99,但是结果不是呢样的,如果你tableview能显示10个cell呢tag应该只有10,在你向下拉的时候更改的仅仅是显示的内容。具体的重用机制还需要更深入研究。 

4.NSArray初始化的时候,今天很晕,刚开始做的第一个程序我在初始化NSArray的时候居然这样搞了一下 
NSArray *array = [[NSArray alloc]initWithObjects:(@"a",@"b",nil)];编译也没有错误,运行没结果,呵呵。去掉括号,没问题了。受C的影响。 

5。向UINavigation添加tableView。 
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)]; 
[table setDelegate:self]; 
[table setDataSource:self]; 
[self.view addSubview:table]; 

6.得到Button的title 
UIButton *but = [[UIbutton alloc] init]; 
[but setTitle:@"title" forState:UIControlStateNormal]; 
[but addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 
-(void)click:(id)sender 

NSString *s = [sender titleForState:UIControlStateNormal]; 


7.对字符串的截取操作。按照“|”实现分割,去掉后面的数字只要前面部分 
NSString *s = @"哈根达斯,4223|帝王蟹,3717|三文鱼,3255|提拉米苏,2733|生蚝,1860|烤鳗,1451|生鱼片,1430|龙虾色拉,1252|北极贝,1230|鲍鱼片,772|芒果冰沙,730|烤鳗鱼,706|龙虾沙拉,692|海鲜泡饭,600|冰淇淋,508|芒果汁,499|鲍片,357|佛跳墙,308|鲍鱼,295|HGDS,262 "; 
    NSMutableArray *array = [[s componentsSeparatedByString:@"|"]retain]; 
    NSMutableArray *mutablearray = [[NSMutableArray alloc] init]; 
    int i = 0; 
    for(i; i<[array count]; i++) 
    { 
        NSString *s1 = [array objectAtIndex:i]; 
        NSArray *a = [s1 componentsSeparatedByString:@","]; 
        [mutablearray addObject:[a objectAtIndex:0]]; 
    } 
    for(i =0 ; i<[mutablearray count]; i++) 
    { 
        NSLog([NSString stringWithFormat:@"%@",[mutablearray objectAtIndex:i]]); 
    } 

8.如何修改Xcode中Copyright 2009 __MyCompanyName__. All rights reserved.的名称。 
以前在CC上见LV发过这个帖子,当时修改成功了,但是重装了一下系统,又挂了,显示的还是__MyCompanyName__,今天又被找到了,记下来呵呵。 
defaults write com.apple.xcode PBXCustomTemplateMacroDefinitions '{ ORGANIZATIONNAME = "名字"; }' 

9.将音频转换成CAF格式 
在mac上使用afconvert命令可以将音频文件转换成caf文件。 

afconvert -f caff -d LEI16@44100 -c 1 in.wav out.caf 

-f 指定文件格式 
-d 数据格式,@44100指采样率 
-c 通道数 

10.怎样从core graphics获取UIImage 
- (void)viewDidLoad 

    [super viewDidLoad]; 

    UIGraphicsBeginImageContext(CGSizeMake(20,20)); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(ctx); 
    CGContextAddArc(ctx, 10, 10, 10, 0, 2*M_PI, 1); 
    CGContextSetRGBFillColor(ctx, 1,0, 0, 1); 
    CGContextFillPath(ctx); 
    UIImage *redBall = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageView *redBallView = [[UIImageView alloc] initWithImage:redBall]; 
    redBallView.center = CGPointMake(160,330); 
    [self.view addSubview:redBallView]; 


11.UIImage imageNamed和UIImage imageWithContentsOfFile的区别 
UIImage imageNamed将为这个图像提供缓存,所以如果你再次需要同一图像时,图像将被从内部缓存而 
不是从资源中加载。而坏消息也是iPhone将为此图像提供缓存。这样如果图片过多就好出现内存泄露的现象。 
UIImage imageWithContentsOfFile将不提供缓存。 

12.在使用IB添加NAvigation的时候出现白屏。 
刚开始的时候我把window给删掉了,结果出来就是白屏,没有东西,什么都没有,忘记还要关联App Delegate里面的window到window了。如果使用IB创建ViewController.xib是可以完全删除的。不影响运行结果。 

13.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath可以更改cell的高度,利用indexPath row可以随意更改每一行的高度例如 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

if([indexPath row] == 2) 
return 80; 
else { 
return 100; 



14.设置IPhone程序的启动画面 
在XCode的项目的resource文件夹中追加名为Default.png的图片就可以了。你可以使用Organizer工具来抓取iPhone实机上图片。 
Organizer是XCode中内含的一个工具,通过选择菜单XCode -> Window -> Organizer来启动。这样就会在程序加载前的呢一会黑屏中运行该画面! 

15.播放caf音频文件 
引入AudioToolbox.framework。导入音频文件,声明变量SystemSoundID sound1; 
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"caf"];获取音频文件路径 
CFURLRef sound1URL = (CFURLRef)[NSURL fileURLWithPath:path1];将路径转换为CFURLRef 
AudioServicesCreateSystemSoundID(sound1URL, &sound1);加载音频文件并与指定soundID联系起来 
然后在需要播放的地方加入AudioServicesPlayAlertSound(sound1);就可以了。 

16.将view设置成圆角 
今天从CC上看到的,实验了一下,效果挺好的。呵呵 
首先导入QuartzCore.framework,#import <QuartzCore/QuartzCore.h> 
然后添加下面两行代码: 
view.layer.cornerRadius = 8;//圆角 
view.layer.masksToBounds = YES;//在所在的层绘制圆角 

17.设置坐标和大小 
CGPoint a = CGPointMake(10,20); 
CGSize b = CGSizeMake(40,40); 
CGRect aa = CGRectMake(10, 30, 240, 240); 

18.把UIColor转换为CGColor 
UIColor *redColor = [UIColor redColor]; 
CGColor *RedColor = redColor.CGColor; 

19.CALayer就是层,这个层你随便控制他的大小,旋转,角度,坐标变化或者内容之类的信息,这些变化还可以通过动画表现出来。UIView所有你能看到的显示的内容,后面都有一个Layer。下面来自定义添加一个CALayer。 
CALayer *layer = [[CALayer alloc] init];//定义一个layer 
CGRect aa = CGRectMake(10, 30, 240, 240);//设置该layer的坐标和大小 
layer.frame = aa; 
UIColor *c = [UIColor redColor]; 
[layer setBackgroundColor:(c.CGColor)];//设置该layer的背景,因为layer setBackgroundColor用到的是CGColor所以要进行一次转换 
[self.view.layer insertSublayer:layer atIndex:0];//在self view中添加该layer 

20.在使用Xcode编写程序的时候,在类方法上面option + 双击就可以跳到Document中的相关说明下面,很方便! 

21.命令行解压tgz文件: 
tar xzvf filename

原文地址:https://www.cnblogs.com/chenfulai/p/1965253.html