Iphone开发问题集

来源:互联网 发布:windows开启samba 编辑:程序博客网 时间:2024/04/24 18:05

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

22.从网络下载图片并显示,原以为很难,今天看到UIImage有imageWithData这个方法,试一下就出来了。

NSURL *url = [NSURL URLWithString:@"XXXX"];
NSData *picData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:picData];
[imageView setImage:img];

23.NSTimer传参数。

我们在使用NSTimer定时的时候有的时候需要传过去一个参数。我们可以这样来传参数。

首先定义NSTimer

NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(onTimer:) userInfo:parameter repeats:YES];

然后再onTimer事件中使用[time userInfo];就可以了。传什么类型在onTimer中使用对应类型就好了。

24.关于线程。在自己创建的子线程中不可以对控件进行操作。线程中需要使用NSAutoreleasePool。

[NSThread detachNewThreadSelector:@selector(threadMethod:) toTarget:self withObject:nil];

-(void)threadMethod:(NSString *)s{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self performSelectorOnMainThread:@selector(method:) withObject:nil waitUntilDone:YES];//这样就可以在主线程中进行控件的操作
[NSThread exit];
[pool release];
}

-(void)method{
………………
}

25.UIAlertView提示上面按钮触发的事件,根据选择的按钮触发不同的事件。

[alert addButtonWithTitle:@"YES"];
[alert addButtonWithTitle:@"NO"];

YES按钮返回的buttonIndex是0,NO按钮返回的buttonIndex是1这样就可以在下面的委托事件中使用了

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

26.更改cell选中后的颜色

cell.selectionStyle = UITableViewCellSelectionStyleGray;

27.使用IB添加tabbarcontroller后crash问题

用ib添加一个tabbarcontroller,里面放5个tabbaritems,关联5个xib文件。

Iphone开发问题集(不定时更新)
其中5个xib文件都是OK的。想着就ok了,结果运行的时候只要每一个view里面有controller操作,就会crash。实在是找不到原因,最 后请教高人,原来是IB弄错了。关联tabbaritems后还有关联相应的controller类。

在这里:

Iphone开发问题集(不定时更新)
其中class里面默认的是UIViewController,这里必须选择每一个view对应的controller类。

28.让label自适应里面的文字,自动调整宽度和高度。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。

[label setNumberOfLines:0];

NSString *s = @”string……”;

UIFont *font = [UIFont fontWithName:@"Arial" size:12];

CGSize size = CGSizeMake(320,2000);

CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];

[self.view addSubView:label];

这样就可以对s赋值让其自动调整其大小了。其实宽度可以设置为固定,让其改高度更好。呵呵

29.在tableview中选种一行,当失去焦点的时候选 种行的颜色自动消失。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

[table  deselectRowAtIndexPath:[table  indexPathForSelectedRow] animated:YES];

30。有用的色彩搭配表

http://www.print100.com/hk/html/csi.aspx

 

http://blog.csdn.net/iphoneing/article/details/5891334

原创粉丝点击