iOS 索引列 使用詳解

做蘋果開發的朋友在地區列表可能會遇到在頁面的右側有一列類似與導航的索引列,這次有機會遇到了,細細研究了一下,原來沒有想象中的高達上,只需要簡單的幾步就能做出自己的索引列。

,關注我的博客的朋友可能會對這張圖片比較熟悉,我在上一篇博客,關于搜索條的使用中,也用到了這張圖片,這是我在做一款仿照美團購物軟件中用到的實例圖,還是比較有說服力的。本來想和搜索條在一塊講解,后來考慮了一下,這個東西和搜索條功能雖有相似之處,卻并非需要一起使用,所以就單獨摘出來,獨立介紹吧!
  雖然看著很高大上,效果確實挺不錯的。這個既不需要引入第三方的類庫,還不需要單獨的委托,它是uitableview列表控件的一個功能的延伸,將用戶的體驗做到極致,這也就是蘋果細致、人性化的地方。下面開始關于索引列的講解。
  第一步:添加列表委托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];
//設置索引列文本的顏色
myTableView.sectionIndexColor = BB_Yanzheng_Color;
//myTableView.sectionIndexBackgroundColor=BB_Red_Color;
//myTableView.sectionIndexTrackingBackgroundColor=BB_White_Color;

[self.view addSubview:myTableView];

這里有個需要注意的地方,也是我花費了一段時間才總結出來的經驗,右側索引列的文本顏色是可以自定義改變的 myTableView.sectionIndexColor = BB_Yanzheng_Color。只需要設置這個屬性即可,當初花費了我不少精力,差點自定義去設置,偶然間發現原來蘋果已經自定義好了這個屬性,所以以后還是得從源頭上解決問題。
  第三步:索引列數據的獲取

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

{

    //當前字母

    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]];

    }

}

第四步:相關委托的使用

//添加索引列

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

{

if (tableView == self.searchDisplayController.searchResultsTableView)

{

    return nil;

}



return suoyinCityList;

}

//索引列點擊事件

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

{

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

//點擊索引,列表跳轉到對應索引的行

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

//彈出首字母提示

//[self showLetter:title ];

return index+4;

}

這里注釋的已經很詳細,基本不需要我多解釋,唯一需要注意的地方是如果本頁面里面有多個列表的話需要在不需要的列表中隱藏索引列,否則可能會出現奇怪的問題,主要是獲取不到數據,因為索引列是和uitableview的

  • (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)
    {
    //標題背景
    UIView *biaotiView = [[[UIView alloc]init]autorelease];
    biaotiView.backgroundColor = BB_White_Color;
    biaotiView.frame=CGRectMake(0, 0, 320, 30);
    [headView addSubview:biaotiView];

      //標題文字
      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;
    }
    配合使用的,這個注意一下就好。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容