溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS 索引列 使用詳解

發(fā)布時(shí)間:2020-07-23 22:07:52 來源:網(wǎng)絡(luò) 閱讀:612 作者:kuuailetianzi 欄目:移動(dòng)開發(fā)

    做蘋果開發(fā)的朋友在地區(qū)列表可能會(huì)遇到在頁面的右側(cè)有一列類似與導(dǎo)航的索引列,這次有機(jī)會(huì)遇到了,細(xì)細(xì)研究了一下,原來沒有想象中的高達(dá)上,只需要簡單的幾步就能做出自己的索引列。iOS 索引列 使用詳解,關(guān)注我的博客的朋友可能會(huì)對這張圖片比較熟悉,我在上一篇博客,關(guān)于搜索條的使用中,也用到了這張圖片,這是我在做一款仿照美團(tuán)購物軟件中用到的實(shí)例圖,還是比較有說服力的。本來想和搜索條在一塊講解,后來考慮了一下,這個(gè)東西和搜索條功能雖有相似之處,卻并非需要一起使用,所以就單獨(dú)摘出來,獨(dú)立介紹吧!

  雖然看著很高大上,效果確實(shí)挺不錯(cuò)的。這個(gè)既不需要引入第三方的類庫,還不需要單獨(dú)的委托,它是uitableview列表控件的一個(gè)功能的延伸,將用戶的體驗(yàn)做到極致,這也就是蘋果細(xì)致、人性化的地方。下面開始關(guān)于索引列的講解。

  第一步:添加列表委托UITableViewDataSource,UITableViewDelegate

  第二步:列表控件的添加

self.myTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, UI_View_Hieght+64) style:UITableViewStylePlain]autorelease];

    [myTableView setBackgroundColor:BB_Back_Color_Here_Bar];

    [myTableView setBackgroundView:nil];

    myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    myTableView.dataSource = self;

    myTableView.delegate = self;

    myTableView.allowsSelection=YES;

    //[myTableView setScrollEnabled:NO];

    myTableView.showsHorizontalScrollIndicator = NO;

    myTableView.showsVerticalScrollIndicator = NO;

    //[XtomFunction addbordertoView:myTableView radius:8.0f width:0.0f color:BB_White_Color];

    //設(shè)置索引列文本的顏色

    myTableView.sectionIndexColor = BB_Yanzheng_Color;

    //myTableView.sectionIndexBackgroundColor=BB_Red_Color;

    //myTableView.sectionIndexTrackingBackgroundColor=BB_White_Color;

    

    [self.view addSubview:myTableView];

這里有個(gè)需要注意的地方,也是我花費(fèi)了一段時(shí)間才總結(jié)出來的經(jīng)驗(yàn),右側(cè)索引列的文本顏色是可以自定義改變的 myTableView.sectionIndexColor = BB_Yanzheng_Color。只需要設(shè)置這個(gè)屬性即可,當(dāng)初花費(fèi)了我不少精力,差點(diǎn)自定義去設(shè)置,偶然間發(fā)現(xiàn)原來蘋果已經(jīng)自定義好了這個(gè)屬性,所以以后還是得從源頭上解決問題。

  第三步:索引列數(shù)據(jù)的獲取

 

for(char c ='A';c<='Z';c++)

 

    {

 

        //當(dāng)前字母

 

        NSString *zimu=[NSString stringWithFormat:@"%c",c];

 

        if (![zimu isEqualToString:@"I"]&&![zimu isEqualToString:@"O"]&&![zimu isEqualToString:@"U"]&&![zimu isEqualToString:@"V"])

 

        {

 

            [suoyinCityList addObject:[NSString stringWithFormat:@"%c",c]];

 

        }

 

}

 

  第四步:相關(guān)委托的使用

 

//添加索引列

 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

 

{

 

    

 

    if (tableView == self.searchDisplayController.searchResultsTableView)

 

    {

 

        return nil;

 

    }

 

 

 

    return suoyinCityList;

 

}

 

//索引列點(diǎn)擊事件

 

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

 

{

 

NSLog(@"===%@  ===%d",title,index);

 

//點(diǎn)擊索引,列表跳轉(zhuǎn)到對應(yīng)索引的行

 

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index+4] atScrollPosition:UITableViewScrollPositionTop animated:YES];

 

    

 

//彈出首字母提示

 

//[self showLetter:title ];

 

return index+4;

 

}

 

這里注釋的已經(jīng)很詳細(xì),基本不需要我多解釋,唯一需要注意的地方是如果本頁面里面有多個(gè)列表的話需要在不需要的列表中隱藏索引列,否則可能會(huì)出現(xiàn)奇怪的問題,主要是獲取不到數(shù)據(jù),因?yàn)樗饕惺呛蛈itableview的

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    if (tableView == self.searchDisplayController.searchResultsTableView)

    {

        return nil;

    }

    UIView *headView = [[[UIView alloc]init]autorelease];

    headView.backgroundColor = [UIColor clearColor];

    if (section!=0)

    {

        //標(biāo)題背景

        UIView *biaotiView = [[[UIView alloc]init]autorelease];

        biaotiView.backgroundColor = BB_White_Color;

        biaotiView.frame=CGRectMake(0, 0, 320, 30);

        [headView addSubview:biaotiView];

        

        //標(biāo)題文字

        UILabel *lblBiaoti = [[[UILabel alloc]init]autorelease];

        lblBiaoti.backgroundColor = [UIColor clearColor];

        lblBiaoti.textAlignment = NSTextAlignmentLeft;

        lblBiaoti.font = [UIFont systemFontOfSize:15];

        lblBiaoti.textColor = [UIColor blackColor];

        lblBiaoti.frame = CGRectMake(15, 7.5, 200, 15);

        lblBiaoti.text = [headerList objectAtIndex:section-1];

        [biaotiView addSubview:lblBiaoti];

    }

    return headView;

}

配合使用的,這個(gè)注意一下就好。


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI