什么時候使用到storyBoard?
- 描述靜態單元格
- 描述多個控制器的時候
監聽TextField的編輯3種方式
- 代理:注意不要讓自己成為自己的代理
- target
- 通知
設置TextField占位文字的顏色
方案一
- 拿到占位文字控件(UILabel),設置UILabel的文字顏色,但是發現系統的TextFiel并沒有向外提供這個屬性,也就是這個屬性是私有的,我們可以通過斷點調試的方法來獲取這個TextFiel的私有屬性名,然后,通過KVC來獲取它的值,然后改變文字顏色
UILabel *placeLabel = [self valueForKey:@"placeholderLabel"];
placeLabel.textColor = [UIColor whiteColor];
- 通過斷點條調試的方法獲取一個對象的私有屬性的方法如下
方案二:使用runtime
在TextField的分類中寫一個分類屬性
#import <UIKit/UIKit.h>
@interface UITextField (Placeholder)
@property UIColor *placeholderColor;
@end
- 在分類的.m文件中實現以下方法
//交換方法
+(void)load
{
Method M1 = class_getInstanceMethod(self, @selector(setHMX_placeholder:));
Method M2 = class_getInstanceMethod(self, @selector(setPlaceholder:));
method_exchangeImplementations(M1, M2);
}
//設置占位文字
-(void)setHMX_placeholder:(NSString *)placeholder
{
//設置占位文字
[self setHMX_placeholder:placeholder];
//設置占位文字顏色
[self setPlaceholderColor:self.placeholderColor];
}
//設置占位文字顏色
-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
//1.把文字顏色先保存起來
objc_setAssociatedObject(self, @"placeholderColor",placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//2.等真正設置文字的時候再去設置文字顏色
//獲取文字控件
UILabel *place = [self valueForKey:@"placeholderLabel"];
place.textColor = placeholderColor;
}
//獲取文字顏色
-(UIColor *)placeholderColor
{
return objc_getAssociatedObject(self, @"placeholderColor");
}
方案三
//設置占位文字的顏色(第一個參數表示要給哪個文字設置富文本屬性)
NSMutableDictionary *attri = [NSMutableDictionary dictionary];
attri[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attri];
分組樣式的TableView注意點
- 如果TableView是分組樣式,默認每一組都會有頭部和尾部間距,可以通過以下的屬性來設置間距的大小
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 10;
- 分組樣式的第一個cell的frame的y值自動設置為35,如果想將所有的cell往上移動,可以通過contentInset屬性來設置
self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
不能去手動設置TableView的滾動范圍
- tableView的滾動范圍是系統自動根據內容去計算的,如果我們自己手動去設置,會出問題
- 例如:
//請求數據
//collectionView作為TableView的底部視圖,開始的時候設置collectionView的尺寸為0,當collectionView的數據加載完畢后,重新計算collectionView的高度,讓TableView重新計算滾動范圍,實現滾動
-(void)loadData
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *prame = [NSMutableDictionary dictionary];
prame[@"a"] = @"square";
prame[@"c"] = @"topic";
[manager GET:@"http://url123" parameters:prame progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {//成功后回調
//解析數據
self.squareArray = [HMXSqaureItem mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
//重新計算collectionView的高度(計算出有多少行cell)
NSInteger count = self.squareArray.count;
//行數
NSInteger rows = (count - 1)/cols + 1;
//collectionView的高度
CGFloat collectionH = rows * itemWH + (rows - 1) * margin;
self.collectionView.height = collectionH;
//刷新collectionView
[self.collectionView reloadData];
//重新設置tableView的滾動范圍
self.tableView.tableFooterView = self.collectionView;
//注意:當collectionView的數據加載完畢后,不能使用下面的方法手動設置TableView的contentSize,如果設置了,在當前頁面,滾動范圍會是準確的,但是當跳轉到下一個控制器后再回來,發現contentSize又會恢復原來的尺寸,這是因為,TableView在顯示的時候會根據當前的內容自動計算滾動范圍,在一開始collectionView的尺寸為0,因此TableView不能滾動,手動設置后,跳轉到下一個控制器再回來,TableView在即將顯示的時候又會根據當前的內容自動計算滾動范圍,而當時設置的collectionView尺寸為0,因此不能滾動,恢復了原樣
// self.tableView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.collectionView.frame));
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {//失敗后回調
HMXLog(@"%@",error);
}];
}
能打開網頁的控制器或View
-
1.safari
- 缺點:跳轉到safari應用,離開當前應用
- 優點:有很多自帶的功能,前進,后退,刷新,網址
-
2.UIWebView
- 優點:在當前應用就可以打開
- 缺點:前進,后退,刷新等這些功能必須要手動去實現,而且進度條做不了,以前在WebView上面看到的進度條都是假象
-
3.WKWebView(UIWebView的升級版)
- 優點:在當前應用就可以打開,自帶前進,后退,刷新等功能,并且可以監聽真正的進度
-
4.SFSafariViewController
- iOS9新出來的控制器,在當前應用打開網頁,功能同safari一樣,這個控制器在storyboard中還沒有集成,所以只能通過代碼手動創建,使用時:導入頭文件#import <SafariServices/SafariServices.h>
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:url];
//一定要使用modal的方式跳轉到這個控制器,因為蘋果在方法的聲明前寫的很清楚,當使用modal的方式的時候,在modal控制器點擊Done的時候系統會自動dismiss掉modal控制器控制器回到上一個控制器
[self presentViewController:safari animated:YES completion:nil];
抽取業務類的規范
在.h文件的頭部說明該工具類主要用來干嘛
在方法的聲明前最好寫上有提示功能的注釋,方便別人使用
.h文件
// 該類專門用于文件的處理
#import <Foundation/Foundation.h>
@interface FileManager : NSObject
/**
* 獲取文件夾尺寸
*
* @param directoryPath 文件夾全路徑
*
* @return 文件夾的尺寸
*/
+(NSInteger)getDirectorySize:(NSString *)directoryPath;
/**
* 刪除文件夾下所有文件
*
* @param directoryPath 文件夾全路徑
*/
+(void)removeDirectoryPath:(NSString *)directoryPath;
@end
- .m文件
#import "FileManager.h"
@implementation FileManager
//獲取文件大小
+(NSInteger)getDirectorySize:(NSString *)directoryPath
{
//獲取文件管理者
NSFileManager *fileManager = [NSFileManager defaultManager];
//如果用戶傳入的不是文件夾或者該文件不存在就報錯
BOOL isDirectory;
BOOL isExist = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isExist || !isDirectory)
{
NSException *exception = [NSException exceptionWithName:@"filePathError" reason:@"請傳入符合規范的文件夾路徑" userInfo:nil];
[exception raise];
};
//獲取這個文件路徑下所有的文件
NSArray *subPaths = [fileManager subpathsAtPath:directoryPath];
NSInteger totalSize = 0;
//遍歷數組
for (NSString *subPath in subPaths) {
//拼接全文件路徑
NSString *fileFullPath = [directoryPath stringByAppendingPathComponent:subPath];
//排除文件夾和不存在的文件
BOOL isDirectory;
BOOL isExist = [fileManager fileExistsAtPath:fileFullPath isDirectory:&isDirectory];
if (!isExist || isDirectory) continue;
//排除隱藏文件
if ([fileFullPath containsString:@".DS"] ) continue;
//獲取指定路徑下的文件屬性
NSDictionary *artt = [fileManager attributesOfItemAtPath:fileFullPath error:nil];
NSInteger size = [artt fileSize];
totalSize += size;
}
return totalSize;
}
//刪除文件
+(void)removeDirectoryPath:(NSString *)directoryPath
{
//獲取文件管理者
NSFileManager *fileManager = [NSFileManager defaultManager];
//如果用戶傳入的不是文件夾或者該文件不存在就報錯
BOOL isDirectory;
BOOL isExist = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isExist || !isDirectory)
{
NSException *exception = [NSException exceptionWithName:@"filePathError" reason:@"請傳入符合規范的文件夾路徑" userInfo:nil];
[exception raise];
};
//獲取cachePath文件路徑下所有的一級文件夾
NSArray *subPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
//拼接全路徑
for (NSString *subPath in subPaths) {
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
//移除該文件夾下所有的文件
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
}
@end