iOS分組索引和索引分區(qū)

1.新建工程名SearchViewController,F(xiàn)ile->New->Projectr->SingleView Application->next

成都創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的黃石港網(wǎng)站建設(shè)公司,黃石港接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行黃石港網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

2.添加協(xié)議和聲明變量

還需要讓類(lèi)遵循UISearchBarDelegate協(xié)議,除了充當(dāng)表視圖的委托之外還需要讓它充當(dāng)搜索欄的委托。

@interface ViewController :UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

{

UITableView*tableavaieGroup;

UISearchBar *search;

NSMutableDictionary *names;

NSMutableArray *keys;

BOOL isSearching;

}

3.添加plist文件,再到ViewDidLoad中初始化視圖

iOS 分組索引和索引分區(qū)

- (void)viewDidLoad

{

//讀取plist文件

NSString *path=[[NSBundlemainBundle]pathForResource:@"Property List"ofType:@"plist"];

NSDictionary *dictionary=[[NSDictionaryalloc]initWithContentsOfFile:path];

//self.names=dictionary;

self.allNames=dictionary;

//讀取字典中的鍵存儲(chǔ)在數(shù)組中

//NSArray *array=[[namesallKeys]sortedArrayUsingSelector:@selector(compare:)];

//self.myKey=array;

   [selfresetSearch];

   [tableViewGroupreloadData];

   [tableViewGroupsetContentOffset:CGPointMake(0.0, 44.0)animated:NO];

[superviewDidLoad];}

4.實(shí)現(xiàn)委托方法

從數(shù)組中讀取字典中的plist文件有幾個(gè)分區(qū)

方法獲取一個(gè)可重用單元,如果單元不存在則創(chuàng)建一個(gè)新的單元。然后從對(duì)應(yīng)查詢(xún)的數(shù)組中獲取對(duì)象,將單元的文本設(shè)置成控制器標(biāo)題并返回單元

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

staticNSString*DisclosureCellIdentifier =@"DisclosureCellIdentifier";

UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:DisclosureCellIdentifier];

if (cell ==nil)

   {//創(chuàng)建一個(gè)新的單元

   cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DisclosureCellIdentifier];

}

//對(duì)應(yīng)查詢(xún)數(shù)組中獲取的對(duì)象,將單元文本設(shè)置成控制器標(biāo)題并返回單元

NSUInteger row=[indexPathrow];

NSString *rowData=[listDataobjectAtIndex:row];

   cell.textLabel.text=rowData;

cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

return cell;

}

5.添加搜索欄委托方法

搜索欄有許多在其委托上調(diào)用的方法,當(dāng)用戶(hù)單擊鍵盤(pán)上的返回按鈕或搜索鍵時(shí),將調(diào)用searchBarBookmarkButtonClicked,此方法從搜索欄獲取搜索短語(yǔ),并調(diào)用我們的搜索方法,這個(gè)搜索方法將刪除names中不匹配的名稱(chēng)和keys中的空分區(qū):

-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar

{

NSString*searchTerm=[searchBartext];

   [selfhandleSearchForTerm:searchTerm];

}

運(yùn)行結(jié)果如下:

iOS 分組索引和索引分區(qū)iOS 分組索引和索引分區(qū)

(點(diǎn)擊搜索框進(jìn)行搜索“egg”,出現(xiàn)第二個(gè)頁(yè)面)

6)NSDictinary遵循NSMutableCopying協(xié)議,該協(xié)議返回一個(gè)NSmutableDictionary,但是這個(gè)方法創(chuàng)建的是淺副本。也就是說(shuō)調(diào)用mutableCopy方法時(shí),它將創(chuàng)建一個(gè)新的NSmutableDictionary對(duì)象,該對(duì)象擁有源字典所擁有的所有對(duì)象,他們并不是副本,而是相同的實(shí)際對(duì)象,由于字典中存有數(shù)組,如果我們從副本的數(shù)組中刪除對(duì)象,這些對(duì)象也會(huì)從原字典的數(shù)組中刪除,因?yàn)楦北竞驮炊贾赶蛳嗤膶?duì)象,在這種情況下元數(shù)組是不可改變

的所以無(wú)法刪除對(duì)象,解決這個(gè)問(wèn)題,需要為存有數(shù)組的字典創(chuàng)建一個(gè)深層可變副本,

iOS 分組索引和索引分區(qū)

Next->  之后 Categroy on (NSDictionary) Categrpy(MutableDeepCopy)

此方法創(chuàng)建一個(gè)新的可變字典,然后在原字典中所有的鍵進(jìn)行迭代,為它遇到的每個(gè)數(shù)組創(chuàng)建可變副本。

-(NSMutableDictionary *)mutableDeepCopy

{

NSMutableDictionary *returnDict=[[NSMutableDictionaryalloc]initWithCapacity:[selfcount]];

NSArray *keys=[selfallKeys];

//NSArray *keys=[self ];

//    因?yàn)樵谠械膍utableCopy中,只會(huì)對(duì)第一層對(duì)象進(jìn)行mutable化.

//    比如NSDictionary嵌套NSArray的話(huà).

//    原有的MutableCopy只會(huì)返回NSMutableDictionary,而不會(huì)使其子元素也變?yōu)镹SMutableArray.

//    而這種方法實(shí)際上類(lèi)似遞歸的結(jié)構(gòu).

//循環(huán)讀取復(fù)制每一個(gè)元素

for(id keyin keys)

   {

id oneValue=[selfvalueForKey:key];

id oneCopy=nil;

//如果key對(duì)應(yīng)的元素可以響應(yīng)mutableDeepCopy方法(還是NSDictionary),調(diào)用mutableDeepCopy方法復(fù)制

if([oneValuerespondsToSelector:@selector(mutableDeepCopy)])

           oneCopy=[oneValuemutableDeepCopy];

elseif([oneValuerespondsToSelector:@selector(mutableCopy)])

           oneCopy=[oneValuemutableCopy];

if(oneCopy==nil)

           oneCopy=[oneValuecopy];

       [returnDictsetValue:oneCopyforKey:key];

   }

return returnDict;

}

7)要想在運(yùn)行時(shí)搜索框后有一個(gè)Cancel按鈕只需在Option中勾選Shows Cancel Button即可,如下圖:

iOS 分組索引和索引分區(qū)

當(dāng)用戶(hù)單擊Cancel按鈕時(shí),程序會(huì)將搜索短語(yǔ)設(shè)置為空,然后重置搜索,并重新加載數(shù)據(jù)一顯示所有名稱(chēng),此外,還要讓搜索欄放棄第一響應(yīng)者狀態(tài),這樣鍵盤(pán)就不再起作用,以便于用戶(hù)重新處理表視圖。

本文題目:iOS分組索引和索引分區(qū)
本文路徑:http://muchs.cn/article18/pdppgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、電子商務(wù)外貿(mào)建站、網(wǎng)站收錄云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)