1.如何自定義UITableViewCell
2.QQ折疊效果
3.UISearchController 搜索框
一.如何自定義UITableViewCell
步驟:
1)自己創(chuàng)建一個類MyCell耳舅,繼承自UITableViewCell仙蛉;
2)在第三問cellForRow這個方法中浪谴,把之前的凡是用到UITableViewCell的地方,全部修改成MyCell纷捞;
【Demo】-【1-CustomCell】
從本地的plist文件拿數(shù)據(jù)方法:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Limit" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
成員變量中聲明的bool值似谁,默認(rèn)為NO;
二 QQ折疊
思路:
1)給每個分區(qū)自定義一個頭部視圖【能被點(diǎn)擊】
//點(diǎn)擊頭部視圖匣摘,響應(yīng)方法
//自定義頭部視圖怒医,頭部視圖繼承自UiView
//返回自定義的分區(qū)頭部視圖
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.sectionHeaderArray[section];
}
//返回分區(qū)頭部視圖的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//讓狀態(tài)在展開和收縮之間切換
self.isAppear = !self.isAppear;
[UIView animateWithDuration:0.2 animations:^{
if (self.isAppear == YES) {
//展開,箭頭旋轉(zhuǎn)90度
self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, M_PI_2);
}else
{
//收縮脆丁,箭頭逆時針旋轉(zhuǎn)90度
self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, -M_PI_2);
}
}];
//刷新tableView
self.block();
}
2)根據(jù)點(diǎn)擊頭部視圖來控制是夠要展開還是收縮世舰,如果是收縮,那么讓該分區(qū)返回的行數(shù)為0槽卫;如果是展開狀態(tài)跟压,那么讓該分區(qū)返回的行數(shù)為具體的好友個數(shù); 【** 主要在第二問中實(shí)現(xiàn) **】
3)給頭部視圖加一個屬性BOOL值歼培,用于記錄當(dāng)點(diǎn)擊頭部視圖之后震蒋,是要收縮還是展開;每次點(diǎn)擊之后丐怯,修改BOOL的值喷好,并刷新tableView;
【Demo】-【2-QQ_ZheDie】
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//折疊效果的核心思想
SectionHeaderView *view = self.sectionHeaderArray[section];
CategoryModel *model = self.dataArray[section];
return view.isAppear?model.friends.count:0;
}
三 搜索
搜索出結(jié)果后读跷,傳入一個需要展示的數(shù)組(攜帶數(shù)據(jù))梗搅,在一個獨(dú)立的tableView中展示出來的;
-(void)creatSearchBar
{
//創(chuàng)建顯示搜索結(jié)果的控制器對象
self.sResultVC = [[SearchResultTableViewController alloc] init];
//1.創(chuàng)建一個UISearchControler對象,并制定顯示搜索結(jié)果的tableView
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.sResultVC];
//2.將搜索控制器跟當(dāng)前的self進(jìn)行關(guān)聯(lián)
self.searchController.searchResultsUpdater = self;
//3.searchBar自適應(yīng)位置
[self.searchController.searchBar sizeToFit];
//4.把searchBar顯示self.tableView在頭部視圖
self.tableView.tableHeaderView = self.searchController.searchBar;
//5.表示該視圖控制器的視圖是否被覆蓋
self.definesPresentationContext = YES;
}
pragma mark -UISearchResultsUpdating
//當(dāng)用戶輸入文本的時候開始調(diào)用
-
(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSLog(@"%@",searchController.searchBar.text);
//把搜索出來的符合條件的結(jié)果存在一個數(shù)組中
NSMutableArray *resultArray = [NSMutableArray array];
for (CategoryModel *model in self.dataArray) {
for (FriendsModel *fModel in model.friends) {//字符串的查找 NSRange range = [fModel.name rangeOfString:searchController.searchBar.text]; if (range.length>0) { //找到了符合條件的fModel无切,然后加入數(shù)組中 [resultArray addObject:fModel]; } }
}
//將搜索之后的結(jié)果交給顯示搜索結(jié)果的控制器顯示出來
self.sResultVC.resultArray = resultArray;
[self.sResultVC.tableView reloadData];
}
根據(jù)字符串長度重新計算frame
//第一個參數(shù):最大的寬和最大的高度荡短,達(dá)不到這個高度會自動縮小 是進(jìn)行自適應(yīng)的尺寸
//第二個參數(shù) option 布局格式 NSStringDrawingUsesLineFragmentOrigin
option:文本繪制時的附加選項(xiàng)
有四種類型:
1.NSStringDrawingTruncatesLastVisibleLine :
2.NSStringDrawingUsesLineFragmentOrigin :
- NSStringDrawingUsesFontLeading :
- NSStringDrawingUsesDeviceMetrics :
通常使用NSStringDrawingUsesLineFragmentOrigin,如果options參數(shù)為NSStringDrawingUsesLineFragmentOrigin哆键,那么整個文本將以每行組成的矩形為單位計算整個文本的尺寸掘托。
如果為NSStringDrawingTruncatesLastVisibleLine或者NSStringDrawingUsesDeviceMetric,那么計算文本尺寸時將以每個字或字形為單位來計算
如果為NSStringDrawingUsesFontLeading則以字體間的行距(leading籍嘹,行距:從一行文字的底部到另一行文字底部的間距闪盔。)來計算
//第三個參數(shù):將文本UIFront存入字典傳到這里 字符串的屬性
字典@{NSFontAttributeName:[UIFont systemFontOfSize:17]};
//第四個參數(shù):context:context上下文。包括一些信息辱士,例如如何調(diào)整字間距以及縮放泪掀。最終,該對象包含的信息將用于文本繪制颂碘。該參數(shù)可為 nil
CGRect rect = [str1 boundingRectWithSize:CGSizeMake(320, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];