1.主线程与分线程
在加载图片比较慢的情况下,先在主线程中设置一个默认图片,然后在分线程中处理实际图片的加载。
例如:
主线程中:
[headImageView setImage:[UIImage imageNamed:@"default_Image.png"]];
//监测分线程
[NSThread detachNewThreadSelector:@selector(getHeaderImage:)toTarget:self withObject: headImageView];
分线程中:
//获取实际头像图片。
- (void)getHeaderImage:(UIImageView*)imageView
{
//imageWithContentsOfFile:在图片比较大的情况下,该函数获取图片有延时。
UIImage *tmpHeadImg = [UIImage imageWithContentsOfFile:[ContactCommon getDocumentPath:source.icon]];
NSArray *info = [NSArray arrayWithObjects:imageView, tmpHeadImg, nil];
[self performSelectorOnMainThread:@selector(setHeaderImage:) withObject:info waitUntilDone:YES];
}
//显示实际头像图片。
- (void)setHeaderImage:(NSArray*)info
{
UIImageView *imgView = (UIImageView *)[info objectAtIndex:0];
UIImage *image = (UIImage *)[info objectAtIndex:1];
imgView.image = image;
}
//将分线程中获取的图片传回主线程。
[self performSelector:@selector(setNewCellImage) onThread:[NSThread mainThread] waitUntilDone:NO];
-(void)setNewCellImage:( id )celldata
{
UIImage *newimage=[celldata objectForKey:@"image"];//从参数celldata里面拿出来图片
[self.DataTable cellForRowAtIndexPath:[celldata objectForKey:@"indexPathdtat"]].imageView.image = newimage;
}
2.//将数据从磁盘读出。
NSUserDefaults *prefers = [NSUserDefaults standardUserDefaults];
self.isHaveCalled = [prefers objectForKey:@"sectionOpen"];
openSectionIndex_ = [[prefers objectForKey:@"sectionIndex"]intValue];
//将数据写入磁盘。
NSUserDefaults *prefers = [NSUserDefaults standardUserDefaults];
[prefers setObject:self.isHaveCalled forKey:@"sectionOpen"];
[prefers setObject:[NSNumber numberWithInt:openSectionIndex_] forKey:@"sectionIndex"];
[prefers synchronize];//记得一定要同步。
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *keyStr = [numberFormatter stringFromNumber:source.cid];
[numberFormatter release];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:isHaveCalled_ forKey:keyStr];
[defaults synchronize];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *keyStr = [numberFormatter stringFromNumber:source.cid];
[numberFormatter release];
if (keyStr !=nil && ![keyStr isEqualToString:@""]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
isHaveCalled_ = [defaults boolForKey:keyStr];
}
3.plain类型的tableView去除空行的singleLine。
在cellforRowAtIndexPath:中加:
(1)去掉下面空行分割线-----tableView.tableFooterView = [[[UIViewalloc]init]autorelease];
(2)去掉上面空行分割线-----tableView.tableHeaderView = [[[UIViewalloc]init]autorelease];
4.//动态计算textView的高度。
- (CGFloat) getTextViewHeight:(NSString *)text fontSize:(CGFloat)fontSize
{
static UITextView *textView = nil;
if (textView == nil) {
textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 180, 100)] autorelease];
textView.font = [UIFont systemFontOfSize:fontSize];
}
textView.text = text;
CGSize textViewSize = [textViewsizeThatFits:CGSizeMake(180, MAXFLOAT)];
return textViewSize.height;
}
5.//拍照页面右下角显示摄像功能按钮。
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//media数组,包括拍照和摄像。如果不需要摄像功能,则将以下两句注释掉即可。
NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
picker.mediaTypes = temp_MediaTypes;
}
6.//Mac上清理svn
打开中断terminal,输入svn cleanup … 其中…代表工程文件的路径。
7.//获取除状态栏之外的屏幕尺寸。
[[UIScreen mainScreen] applicationFrame]
//获取包括状态栏的整个屏幕尺寸。
[[UIScreen mainScreen] bounds]
8.UITableView有一个属性是tableHeaderView,可以将searchBar作为tableView的tableHeaderView
9.获取navigationController的个数----[self.navigationController.viewControllers objectAtIndex:]
10.在头文件.h中定义其它类对象为一个成员变量,需要用 @class + 类名;在实现文件.m文件中定义其它类对象为局部变量,需要用 #import + "类名"。
在头文件中调用普通类时,用 #import + "类名";调用系统类时,用 import + <类名>。
11.Objective-C中
nil:指向oc中对象的空指针
Nil:指向oc中类的空指针
NULL:指向其他类型的空指针,如一个c类型的内存指针,int *pointerInt = NULL;
NSNull:在集合对象中,表示空值的对象
NSNull只有一个方法:null
如果要向一个字典,或数组 加入空对象可以加 [NSNull null];
[dictionary setObject:[NSNull null], forKey:"Email"];
把[NSNull null]"想象"为0,nil是没有,二者是不同的。
[NSNull null]通常作为占位符作用。
12.添加图片时,图片加到本地文件夹的同时,也要添加到工程中。
13.动画给某个点击后淡入淡出事件加个动画。
[UIView animateWithDuration:0.3
animations:^{
self.alpha = 0;
}
completion:^(BOOL finished){
[self removeFromSuperview];
[tempWindow_ resignKeyWindow];
[tempWindow_ release];
VoIP_iPhoneAppDelegate* delegate = (VoIP_iPhoneAppDelegate*)[UIApplicationsharedApplication].delegate;
[delegate.windowmakeKeyWindow];
}];
注意:对于动画消失是先设alpha = 0,再remove掉;而对于动画出现,是先add进来,再设alpha = 1.
14.在当前window上添加一个view,该view要覆盖状态栏。
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320,480/568)];
window.windowLevel =UIWindowLevelAlert; //alert框的优先级,alert框弹出,它的window覆盖了整个window。
[window makeKeyAndVisible]; //让该window成为最上层的window,注意用完了要释放,该例子是在NewFunctionIntroductionView中释放的。
NewFunctionIntroductionView *tipsView = [[NewFunctionIntroductionView alloc] initWithViewController:self andWindow:window andViewIndex:EContactViewController];
[window addSubview:tipsView];
[tipsView release];
15.可以可以设置动画效果的属性
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
16.判断两个点是否为同一个。
BOOL isEqualPoint = CGPointEqualToPoint(point1, point2);
BOOL isEqualSize = CGSizeEqualToSize(size1, size2);
BOOL isEqualRect = CGRectEqualToRect(rect1, rect2);
BOOL isIntersect = CGRectIntersectsRect(rect1, rect2); //判断二者之间是否有交集。
17.UITouch类中包含5个属性:
window:触摸产生时所处的窗口。由于窗口可能发生变化,当前所在的窗口不一定是最开始的窗口。
view:触摸产生时所处的视图。由于视图可能发生变化,当前视图也不一定时最初的视图。
tapCount:轻击(Tap)操作和鼠标的单击操作类似,tapCount表示短时间内轻击屏幕的次数。因此可以根据tapCount判断单击、双击或更多的轻击。
timestamp:时间戳记录了触摸事件产生或变化时的时间。单位是秒。
phase:触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。
18.利用NSUserDefaults存取用戶設定
static NSString *KeySound = @"CubicManSoundKey";
- NSNumber *defaultValue = [NSNumber numberWithInt:1];
- NSDictionary *resourceDict = [NSDictionary dictionaryWithObject:defaultValue forKey:KeySound];
- [[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];
- int GetSoundOn()
- {
- return [[NSUserDefaults standardUserDefaults] integerForKey:KeySound];
- }
19.文件路径
(1)检索Documents目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentDirectory stringByAppendingPathComponent:@"theFile.txt"];
(2)获取tmp目录
NSString *tempPath = NSTemporaryDirectory();
NSString *tempFileName = [tempPath stringByAppendingPathComponent:@"tempFile.txt"];
20.iPhone5在程序中添加启动页面。
(1)将图片添加到工程中,该图片必须命名为Default-568h@2x.png;
(2)将图片拖拽到工程文件Summary--->Launch Images中。
21.可以保存数据类型的节点:NSDictionary, NSString, NSNumber, NSDate, Boolean;
NSDictionary中的键key必须是NSString类型的,值value可以为任何类型(如中英文plist)。
22.获取系统地址簿数据
BOOL isSystemAddBookEmpty =YES;
//得到系统地址簿,判断是否为空。
ABAddressBookRef addressRef = ABAddressBookCreate();
int totalCount = ABAddressBookGetPersonCount(addressRef);
if (totalCount != 0) {
isSystemAddBookEmpty = NO;
}
23.从iPhone/iPod将图片导入到电脑,则打开电脑上的“图像捕捉工具”。
24.- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
拉伸图片而使图片的四个角不变形,leftCapWidth是左边不拉伸的宽度,(totalWidth -leftCapWidth - 1)是右边不拉伸宽度,topCapHeight上面不拉伸的高度,(totalHeight -topCapHeight - 1)是下面不拉伸的高度,允许拉伸的只是leftCapWidth后的一个竖着的像素,以及topCapHeight下面的一个横着的像素。
25.- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//对于这个函数,如果return 0;则返回系统默认的高度,如果要自己指定sectionHeader的高度,则需要配合使用viewForHeaderInSection:函数
}
如:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (tableView == self.tableView) {
if (section == 1 || section == 2) {
UIImageView *imageview = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 2)]autorelease];
return imageview;
}
}
return nil;
}
因为分组表不光有sectionHeader还有sectionFooter,所以要想指定分组表中两个section之间的距离,则需要同时调用四个函数:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
26.typedef使用
typedef void (^Block)(void);
这样可以用Block作为类型定义变量。
27.//ios4上UITextView开始输入文字时会产生文字漂移。
[self.textView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
28.contentSize,contentOffset与contentInset区别。
(1)contentSize是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个scrollview,它的frame为(0,0,320,480),而它的contentSize为(320,960).也就是说,这个scrollview整个内容的大小为(320,960),要通过上下滑动scrollview来查看(320,480)后的内容。
(2)contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
(3)contentInset是scrollview中contentView.frame.origin与scrollview.frame.origin的关系,比如contentView的frame为(0,30,320,480),那么contentInset则为(30, 0, 0 , 0),即是UIEdgeInsetsMake(top, left, bottom, right).(图片如附件)
29.(1)修改应用程序icon,在info.plist文件中添加或修改Info files,添加或修改一个item;
(2)修改应用程序名称,修改target-->Build Settings-->Product Name;
(3)修改Launch images,只要替换Default@2x.png与Default-568h@2x.png。
30.主线程与分线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//启用分线程,在分线程中处理事务。
BOOL result = [manager_addAll:modelsisTransaction:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//启用主线程,去主线程处理事务。
[BFbroadcastBusinessNotify:MakeID(EDatabaseManager,EAddAllAsync)withInParam:[NSNumbernumberWithBool:result]];
log4Info(@"addAllAsync has finished!");
});
});
31.//过滤searchTerm中的特殊字符,只保留“+、数字、字母和汉字”。31.
NSString *searchTextRegex = @"^[+0-9a-zA-Z\u4E00-\u9FA5]+$";
NSPredicate *searchTextTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", searchTextRegex];
NSString *tempString = @"";
for (int i = 0; i < [searchTermlength]; i++) {
//依次比较字符串中的每个字符,以过滤特殊字符。
NSRange range = NSMakeRange(i, 1);
NSString *character = [searchTerm substringWithRange:range];
if ([searchTextTest evaluateWithObject:character]) {
tempString = [tempString stringByAppendingString:character];
}
}
searchTerm = tempString;