Iphone開發問題集

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

 

 

 

from:@“http://www.wuleilei.net/1135.html”

發佈了45 篇原創文章 · 獲贊 1 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章