ios 方面总结的相关知识(2)

来源:互联网 发布:网页编程网 编辑:程序博客网 时间:2024/05/17 13:40

数组相关:

17  判断数组中是否存在某元素:

BOOL isValue=[keyArray containsObject:@"aaa"];

18.1把字符串按逗号隔开,并保存到数组:

NSArray *keyArray=[[NSArray alloc] init];

keyArray=[@"冬瓜,西瓜,南瓜,苦瓜,丝瓜" componentsSeparatedByString:@","];


18.2把数组中的取出来,拼成用逗号隔开的字符串:

NSString *n=[keyArray componentsJoinedByString:@","];


18.3 NSMutableArray转化成NSArray

NSArray *phoneA=[[NSArray alloc] init];

NSMutableArray *phoneArrayss=[[NSMutableArray alloc] init];

phoneA=[phoneArrayss mutableCopy];


19获取本地时间(大写HH获取24小时制的)

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *timeStr=[formatter stringFromDate: [NSDate date]];


20 UITableView一些属性:

tableView.bounces=NO//禁止拖动

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉边框

[cellView setSelectionStyle:UITableViewCellSelectionStyleNone];//禁止触发点击某行

//实现某行的选中效果(点击某行变颜色,松开还是刚变的颜色,点击其他行这行颜色消失):

[cellView setBackgroundColor:[UIColor clearColor]];    

    cellView.selectedBackgroundView = [[[UIView alloc] initWithFrame:cellView.frame] autorelease];

    UIImageView *ia1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)];

    ia1.image=[UIImage imageNamed:@"ts_bg01.png"];

    cellView.backgroundView=ia1;

    

    UIImageView *ia2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)];

    ia2.image=[UIImage imageNamed:@"ts_bg02.png"];

    cellView.selectedBackgroundView=ia2;


21 通知NSNotificationCenter相关:

21.1定义通知:

NSNotificationCenter *ncn = [NSNotificationCenter defaultCenter];

[ncn addObserver:self selector:@selector(DuiBiShow:) name:@"DuiBiButton" object:nil];


21.2定义通知调用的方法:

- (void)DuiBiShow:(NSNotification*) notification

{

NSMutableArray *Array = [notification object];//通过这个获取到传递的对象

}


21.3调用通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DuiBiButton" object:passDuiBiArray];


21.4 移除name为DuiBiButton的通知:

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DuiBiButton" object:nil];


21.5 移除本ViewControll页的所以通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];


22 UIScrollView属性:

tracking //当 touch 后还没有拖动的时候值是YES,否则NO

zoomBouncing //当内容放大到最大或者最小的时候值是 YES,否则 NO

zooming //当正在缩放的时候值是 YES,否则 NO

decelerating //当滚动后,手指放开但是还在继续滚动中。这个时候是 YES,其它时候是 NO

decelerationRate //设置手指放开后的减速率

maximumZoomScale //一个浮点数,表示能放最大的倍数

minimumZoomScale  //一个浮点数,表示能缩最小的倍数

pagingEnabled //当值是 YES 会自动滚动到 subview 的边界。默认是NO

scrollEnabled //决定是否可以滚动

sView.contentSize = CGSizeMake(320*5,372);//滚动的范围


delaysContentTouches //是个布尔值,当值是 YES 的时候,用户触碰开始,scroll view要延迟一会,看看是否用户有意图滚动。假如滚动了,那么捕捉 touch-down 事件,否则就不捕捉。假如值是NO,当用户触碰, scroll view 会立即触发 touchesShouldBegin:withEvent:inContentView:,默认是 YES


canCancelContentTouches //当值是 YES 的时候,用户触碰后,然后在一定时间内没有移动,scrollView 发送 tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,scrollView 发送了 touchesCancelled:withEvent: 到 subview,然后 scroView 开始滚动。假如值是 NO,scrollView 发送 tracking events 后,就算用户移动手指,scrollView 也不会滚动。


contentSize //里面内容的大小,也就是可以滚动的大小,默认是0,没有滚动效果。

showsHorizontalScrollIndicator //滚动时是否显示水平滚动条

showsVerticalScrollIndicator //滚动时是否显示垂直滚动条

bounces //默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止。

bouncesZoom //和 bounces 类似,区别在于:这个效果反映在缩放上面,假如缩放超过最大缩放,那么会反弹效果;假如是 NO,则到达最大或者最小的时候立即停止。


directionalLockEnabled //默认是 NO,可以在垂直和水平方向同时运动。当值是 YES 时,假如一开始是垂直或者是水平运动,那么接下来会锁定另外一个方向的滚动。 假如一开始是对角方向滚动,则不会禁止某个方向

indicatorStyle //滚动条的样式,基本只是设置颜色。总共3个颜色:默认、黑、白

scrollIndicatorInsets //设置滚动条的位置


23 UILabel自适应:

UILabel *requestlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleTypelabel.frame.origin.y+titleTypelabel.frame.size.height+20, 280, 20)]; 

        UIFont *font2 = [UIFont systemFontOfSize:14];  

        [requestlabel setFont:font2];  

        [requestlabel setNumberOfLines:0];  

        [requestlabel setBackgroundColor:[UIColor clearColor]];      

        NSString *text2 = [NSString stringWithFormat:@"要求: %@",req];

        CGSize size2 = [text2 sizeWithFont:font2 constrainedToSize:CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];  

        CGRect rect2 = requestlabel.frame;  

        rect2.size = size2;  

        [requestlabel setFrame:rect2];  

        [requestlabel setText:text2];  

        [littleScrollView addSubview:requestlabel];


24 数据处理时 弹出的UIAlertView,上面放一个转动的UIActivityIndicatorView。当处理完数据UIAlertView自动消失。

loginAlerView = [[UIAlertView alloc]

                             initWithTitle:nil message:@"数据下载中\n请耐心等待⋯⋯\n\n"

                             delegate:nil cancelButtonTitle:nil

                             otherButtonTitles: nil];

            UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(120, 70, 40, 40)];

            [progressInd startAnimating];

            progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

            [loginAlerView addSubview:progressInd];

            [loginAlerView show];


//UIAlertView自动消失(和点击完取消按钮一样的效果,在需要的时候调用):

[loginAlerView dismissWithClickedButtonIndex:[loginAlerView cancelButtonIndex] animated:YES];


25 去除nsstring中的空格 

NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet ]; 

NSString * username = [mUsernameField stringValue]; 

username = [username stringByTrimmingCharactersInSet :whitespace];


26 实现动画:一个接一个地显示一系列的静态图象: 

NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], nil]; 

UIImageView *myAnimatedView = [UIImageView alloc]; 

[myAnimatedView initWithFrame:[self bounds]]; 

myAnimatedView.animationImages = myImages; //animationImages属性返回一个存放动画图片的数组 

myAnimatedView.animationDuration = 0.25; //浏览整个图片一次所用的时间 

myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 动画重复次数 

[myAnimatedView startAnimating]; 

[self addSubview:myAnimatedView]; 

[myAnimatedView release];


27  UIWebView完整的显示Html:

定义全局变量UIWebView *content;  加入代理UIWebViewDelegate;

//实例化:

content = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, 570, 540)];

    self.content.delegate=self;

    [self.content setUserInteractionEnabled:NO]; // 去掉滚动条并禁止滚动

    [content setBackgroundColor:[UIColor clearColor]];

    [self.noticeScroll addSubview:content];


NSString *htmlId = [NSString stringWithFormat:@"<div id='foo'>%@</div>",caseInfos.Content];//要在你的html里面加foo标记用来计算html的长度。 

    [self.content loadHTMLString:[NSString stringWithFormat:htmlId] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];


//当webView加载完成后调用:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    //    得到html的高度:

    NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];

    [self.content setFrame:CGRectMake(0,60, 570,[output intValue]+30)];//设置UIWebView

    self.noticeScroll.contentSize = CGSizeMake(570, content.frame.origin.y+content.frame.size.height);//设置UIScrollView的滚动范围

}


28 判断是否有网络:

导入附件中的文件,然后在需要判断的页面引入#import "IsNetworking.h";

[attachment=49370] 

需要判断的地方:

IsNetworking *IsNetWork = [[IsNetworking alloc] init];

    BOOL isNets=[IsNetWork isNetworkReachable];

    if(isNets==NO){//没有网络}

    else{//有网络}


29 开发中经常用到的sql语句:

INSERT OR REPLACE INTO PhoneInfo (ID,Logo,Name) VALUES (%d,'%@','%@') //添加

Update MobPhoneInfo ID=%d,Logo='%@',Name='%@' //修改

Delete from PhoneInfo //删除表中所有数据

Delete from PhoneInfo  Where Id=2 //删除表中Id=2的数据


SELECT * FROM PhoneInfo Order By ID desc //按ID从大到小的顺序查询所有数据

SELECT ID FROM PhoneInfo Order By ID desc limit 0,1 //查询最大ID

SELECT * FROM PhoneInfo where Name like '三星' //查询name为三星的所有数据

SELECT * FROM PhoneInfo where NetFlow like '%%上网%%' //查询netflow中包含“上网”的所有数据

SELECT count(Name) num FROM PhoneInfo where Id=2 //查询id=2的数据总数

SELECT * FROM PhoneInfo ORDER BY RANDOM() limit 3 //查询3条随机的数据


30 字符串相关:

30.1追加字符:

NSMutableString *string = [[NSMutableString alloc] init];

string=@“你好”;

[string appendFormat:@"中国"];


30.2字符串替换:把info中所有的<都替换成#

NSString *stroneIntro=[info stringByReplacingOccurrencesOfString:@"<" withString:@"#"];


30.3字符串比较:

Bool IsTrue=[@"Nob"isEqualToString:@"Mob"]


30.4 不考虑大小写比较字符串

NSString *astring01 = @"this is a String!";

NSString *astring02 = @"This is a String!";

BOOL result = [astring01 caseInsensitiveCompare:astring02] = =NSOrderedSame;


30.5 改变字符串的大小写

NSString *string1 = @"A String"; 

NSString *string2 = @"String"; 

NSLog(@"string1:%@",[string1uppercaseString]);//大写

NSLog(@"string2:%@",[string2lowercaseString]);//小写

NSLog(@"string2:%@",[string2capitalizedString]);//首字母大小


30.6在串中搜索子串

NSString *string1 = @"This is a string";

NSString *string2 = @"string";

NSRange range = [string1 rangeOfString:string2];

int location = range.location;

int leight = range.length;

NSString *astring = [[NSString alloc] initWithString:[NSStringstringWithFormat:@"Location:%i,Leight:%i",location,leight]];

NSLog(@"astring:%@",astring);

[astring release];


30.7 抽取子串

//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符

NSString *string1 = @"This is a string";

NSString *string2 = [string1 substringToIndex:3];

NSLog(@"string2:%@",string2);


//-substringFromIndex:以指定位置开始(包括指定位置的字符),并包括之后的全部字符

NSString *string1 = @"This is a string";

NSString *string2 = [string1substringFromIndex:3];

NSLog(@"string2:%@",string2);


//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串

NSString *string1 = @"This is a string";

NSString *string2 = [string1 substringWithRange:NSMakeRange(0,4)];

NSLog(@"string2:%@",string2);


30.8在已有字符串中按照所给出范围和长度删除字符

//deleteCharactersInRange:

NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];

[String1 deleteCharactersInRange:NSMakeRange(0,5)];

NSLog(@"String1:%@",String1);


30.9在已有字符串后面在所指定的位置中插入给出的字符串 

//-insertString: atIndex:

NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];

[String1 insertString:@"Hi! " atIndex:0];

NSLog(@"String1:%@",String1);


30.10 将已有的空符串换成其它的字符串

//-setString:

NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];

[String1 setString:@"Hello Word!"];

NSLog(@"String1:%@",String1);


30.11按照所给出的范围,和字符串替换的原有的字符

//-setString:

NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];

[String1 replaceCharactersInRange:NSMakeRange(0, 4)withString:@"That"];

NSLog(@"String1:%@",String1);


30.12 -判断字符串内是否还包含别的字符串(前缀,后缀)

//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *)aString;

NSString *String1 = @"NSStringInformation.txt";

[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");

[String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES"): NSLog(@"NO");

//02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *)aString,这一点前面在串中搜索子串用到过;


原创粉丝点击