ios-我常用的一些知識(shí)整理

1.tableView的分割線默認(rèn)頂不到邊穴店,但有時(shí)我們需要頂?shù)竭叀?/h6>
#pragma mark - 分割線頂?shù)竭?-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPat{
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
2.去掉多余cell
[self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
3.讓cell選中時(shí)不變色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
4.去掉指定行cell分割線
if (indexPath.row == 4) {
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
5.取消cell選中狀態(tài)
[tableView deselectRowAtIndexPath:indexPath animated:YES]
6.cell動(dòng)畫出現(xiàn)效果
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    rotation.m34 = 1.0/ -600;
    cell.layer.transform = rotation;
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    
    /*
     CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
     
     scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2, 0.2, 1)];
     
     scaleAnimation.toValue  = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
     
     scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
     
     [cell.layer addAnimation:scaleAnimation forKey:@"transform"];
     */
}
7.獲取當(dāng)前cell
   FirstCell *cell = (FirstCell *)[self.tableView cellForRowAtIndexPath:indexPath];
8.更新某個(gè)cell數(shù)據(jù)
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
9.cell點(diǎn)擊完取消灰色背景
[tableView deselectRowAtIndexPath:indexPath animated:YES];
10.點(diǎn)擊View阵谚,讓鍵盤消失
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
11.拼音首字母排序
NSArray *resultArray = [_dataArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        
        return [obj1 compare:obj2 options:NSNumericSearch];
        
    }];
12.判斷一個(gè)時(shí)間距離當(dāng)前時(shí)間過(guò)去了多久
- (NSString *) compareCurrentTime:(NSString *)theTime
{
    //字符串轉(zhuǎn)日期格式
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:theTime];
    //有時(shí)差添加這個(gè)
    //NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    //NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    //NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
    
    NSTimeInterval  timeInterval = [fromdate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    
    NSString *result;
    
    if (timeInterval < 60) {
        
        result = [NSString stringWithFormat:@"1分鐘"];
        
    }else if((temp = timeInterval/60) <60){
        
        result = [NSString stringWithFormat:@"%ld分前",temp];
        
    }else if((temp = temp/60) <24){
        
        result = [NSString stringWithFormat:@"%ld小前",temp];
        
    }else if((temp = temp/24) <30){
        
        result = [NSString stringWithFormat:@"%ld天前",temp];
        
    }else if((temp = temp/30) <12){
        
        result = [NSString stringWithFormat:@"%ld月前",temp];
        
    }else{
        
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
        
    }
    
    return  result;
    
}
13.獲取時(shí)間戳
//獲取系統(tǒng)當(dāng)前的時(shí)間戳
- (NSString *)getTime{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: dat];
    NSDate *localeDate = [dat  dateByAddingTimeInterval: interval];
    NSTimeInterval a=[localeDate timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];//轉(zhuǎn)為字符型
    return timeString;
}
14.二進(jìn)制轉(zhuǎn)JSON
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
15.在AppDelegate中設(shè)置NavigationBar
if ([UIDevice currentDevice].systemVersion.floatValue >= 7.0) {
    //背景顏色
    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
   //設(shè)置字體顏色衔憨,font,大小
    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont fontWithName:@ "HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
    
}
16.登陸時(shí)判斷是不是手機(jī)號(hào)
//注冊(cè)手機(jī)號(hào)判斷
NSString *phoneRegex = @"1[3|5|7|8|][0-9]{9}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
        
BOOL isMatch = [phoneTest evaluateWithObject:_phoneNumber.text];
if (isMatch) {
            
     NSLog(@"手機(jī)號(hào)正確");
            
}
17.判斷郵箱正確宴抚?
-(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
18.退出登陸后跳回主頁(yè)
 [self.tabBarController setSelectedIndex:0]; 
 [self.navigationController popToRootViewControllerAnimated:YES];
19.設(shè)置底部標(biāo)簽欄背景
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W, 49)];
 backView.backgroundColor = BASECOLOR; 
[self.tabBarController.tabBar insertSubview:backView atIndex:0]; 
self.tabBarController.tabBar.opaque = YES;
20.設(shè)置導(dǎo)航欄不透明
self.navigationController.navigationBar.translucent = NO; 
self.tabBarController.tabBar.translucent = NO;
21.設(shè)置陰影
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)]; 
view.backgroundColor = [UIColor whiteColor]; 
view.layer.shadowOpacity = 0.8; 
view.layer.shadowOffset = CGSizeMake(1.0, 1.0); 
[self.view addSubview:view]; 
self.view.backgroundColor = [UIColor whiteColor];
22.跳轉(zhuǎn)下一頁(yè)勒魔,隱藏底部bar
- (void)nextBtnClicked:(UIButton *)btn{ 
  SetUpController *setVC = [[SetUpController alloc] init];
//隱藏底部狀態(tài)欄 
  [setVC setHidesBottomBarWhenPushed:YES];       
  [self.navigationController pushViewController:setVC animated:YES];
}
23.發(fā)送通知,舉個(gè)例子菇曲,用戶登錄成功冠绢,通知其他頁(yè)面新用戶登錄
//登陸成功之后發(fā)起通知
   [[NSNotificationCenter defaultCenter] postNotificationName:@"此通知標(biāo)識(shí)" object:nil];

//寫在需要刷新數(shù)據(jù)的頁(yè)面
  //注冊(cè)通知
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:kNotificationIntrist object:nil];
 //在該頁(yè)面刪除通知
  - (void)dealloc{
     [[NSNotificationCenter defaultCenter]removeObserver:@"此通知標(biāo)識(shí)"];
   }
24.block傳值
 /*a頁(yè)面.h代碼*/
#import <UIKit/UIKit.h>
//評(píng)價(jià)完成刷新頁(yè)面block
typedef void(^commentOverBlock)(BOOL reload);

@interface CommentController :UIViewController

@property (nonatomic,strong) commentOverBlock commentSuccessBlock;
- (void)returnCommentBlock:(commentOverBlock)block;

/*a頁(yè)面.m代碼*/

#import "CommentController.h"
@implementation CommentController
- (void)returnCommentBlock:(commentOverBlock)block{
    
    self.commentSuccessBlock = block;
}
#pragram mark - 評(píng)論完成執(zhí)行方法
- (void)hadCommendSuccess{

    self.commentSuccessBlock(YES);
    
}

/*-------------------------------------------------------------------*/
/*b頁(yè)面代碼*/
CommentController *commentVC = [[CommentController alloc] init];
[commentVC returnCommentBlock:^(BOOL reload) {
        //刷新頁(yè)面
 }];
25.字體導(dǎo)入
1.先下載需要的.ttf字體包,然后添加到項(xiàng)目中
2.在info.plist文件中添加Fonts provided by application字段常潮,在該字段下弟胀,添加字體包名即可
26.全局token
//寫入
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

[ud setObject:uid forKey:@"uid"];

[ud synchronize];

//取出
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

NSString *uid = [ud objectForKey:@"uid"];
27.同時(shí)進(jìn)行多個(gè)網(wǎng)絡(luò)請(qǐng)求,兩個(gè)請(qǐng)求都完成后執(zhí)行下面操作
//信號(hào)量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    //創(chuàng)建全局并行
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //任務(wù)一
    dispatch_group_async(group, queue, ^{
        [self postRequest:^{ //這個(gè)block是此網(wǎng)絡(luò)任務(wù)異步請(qǐng)求結(jié)束時(shí)調(diào)用的,代表著網(wǎng)絡(luò)請(qǐng)求的結(jié)束.
            //一個(gè)任務(wù)結(jié)束時(shí)標(biāo)記一個(gè)信號(hào)量
            dispatch_semaphore_signal(semaphore);
        }];
    });
    //任務(wù)二
    dispatch_group_async(group, queue, ^{
        [self postCommentListRequest:^{
            dispatch_semaphore_signal(semaphore);
        }];
    });
    
    dispatch_group_notify(group, queue, ^{
        
        // 三個(gè)請(qǐng)求對(duì)應(yīng)三次信號(hào)等待
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"ok啦,可以執(zhí)行下面代碼");
    });

- (void)postRequest:(void(^)())finish{
    
    VideoDetailViewModel *model = [[VideoDetailViewModel alloc] init];
    [model getModel:self.parametersModel videoDetailInfo:^(NSArray *array) {
        
        finish();
    } Failure:^(NSError *error) {
        
        finish();
    }];
}

- (void)postCommentListRequest:(void(^)())finish{
    
    VideoDetailViewModel *model = [[VideoDetailViewModel alloc] init];
    [model getModel:self.parametersModel ChatListModel:^(NSArray *array, BOOL haveMore) {
        
        finish();
    } Failure:^(NSError *error) {
        
        finish();
    }];
    
}
28.AFNetWorking設(shè)置返回json格式:
//設(shè)置請(qǐng)求返回格式數(shù)據(jù)為Json
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
29.AFNetWorking設(shè)置請(qǐng)求超時(shí)
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 15.f;
30.解析AFNetWorking返回error
NSLog(@"%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
31.Navgation返回指定頁(yè)
UIViewController *viewCtl = self.navigationController.viewControllers[2];

    [self.navigationController popToViewController:viewCtl animated:YES];
32.tableview section header高度設(shè)置問題

這個(gè)應(yīng)該是新手遇到的比較多的喊式。起因是iOS奇葩的邏輯孵户,如果你設(shè)置header(或者footer)高度是0的話,系統(tǒng)會(huì)認(rèn)為你沒設(shè)置岔留,然后將其設(shè)置為40.f夏哭。所以需要將其設(shè)置為一個(gè)較小的數(shù)

_tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.001f)];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市贸诚,隨后出現(xiàn)的幾起案子方庭,更是在濱河造成了極大的恐慌,老刑警劉巖酱固,帶你破解...
    沈念sama閱讀 211,348評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械念,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡运悲,警方通過(guò)查閱死者的電腦和手機(jī)龄减,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,122評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)班眯,“玉大人希停,你說(shuō)我怎么就攤上這事∈鸢” “怎么了宠能?”我有些...
    開封第一講書人閱讀 156,936評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)磁餐。 經(jīng)常有香客問我违崇,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,427評(píng)論 1 283
  • 正文 為了忘掉前任羞延,我火速辦了婚禮渣淳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘伴箩。我一直安慰自己入愧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,467評(píng)論 6 385
  • 文/花漫 我一把揭開白布嗤谚。 她就那樣靜靜地躺著棺蛛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呵恢。 梳的紋絲不亂的頭發(fā)上鞠值,一...
    開封第一講書人閱讀 49,785評(píng)論 1 290
  • 那天媚创,我揣著相機(jī)與錄音渗钉,去河邊找鬼。 笑死钞钙,一個(gè)胖子當(dāng)著我的面吹牛鳄橘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播芒炼,決...
    沈念sama閱讀 38,931評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼瘫怜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了本刽?” 一聲冷哼從身側(cè)響起鲸湃,我...
    開封第一講書人閱讀 37,696評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎子寓,沒想到半個(gè)月后暗挑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,141評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡斜友,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,483評(píng)論 2 327
  • 正文 我和宋清朗相戀三年炸裆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鲜屏。...
    茶點(diǎn)故事閱讀 38,625評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡烹看,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洛史,到底是詐尸還是另有隱情惯殊,我是刑警寧澤,帶...
    沈念sama閱讀 34,291評(píng)論 4 329
  • 正文 年R本政府宣布也殖,位于F島的核電站土思,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜浪漠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,892評(píng)論 3 312
  • 文/蒙蒙 一陕习、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧址愿,春花似錦该镣、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至娘纷,卻和暖如春嫁审,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赖晶。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工律适, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人遏插。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓捂贿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親胳嘲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子厂僧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,492評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容