標籤:ios sdk tableview sectionindextitlesfo
在做項目的過程中,我遇到這樣一個問題,就是本身的tableview 調用
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
方法的時候,最後幾個位置點擊後不能準確定位,比如說“#” 不管我如何點擊“#”都無法把其對應的清單項目顯示出來,所以我自己在
- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
方法中重寫了一些方法 代碼如下
- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{<span style="white-space:pre"></span> //1.擷取當前index的section的original的y //2.用tableview.contentsize.height減去y,得到lefty //3.如果lefty>=tableview.frame.size.height 滾動lefty個單位 //4.如果lefty<tableview.frame.size.height 滾動tableview.contentsize.height-tableview.frame.size.height float y = [self getYOffSet:index title:title]; if (tableView.contentSize.height-y>=tableView.frame.size.height) { [tableView setContentOffset:CGPointMake(0, y) animated:NO]; }else{ [tableView setContentOffset:CGPointMake(0, tableView.contentSize.height-tableView.frame.size.height) animated:NO]; } return NSNotFound;}-(float)getYOffSet:(NSInteger)index title:(NSString *)title{
//這裡的offy = 100 是我在這個tableview最上面加了兩個section 不在這個計算之內 顯示了別的東西 對於不需要添加特別提示等//顯示,可以設定為0
float offY = 100; int count = 0;
//對應的所有內容的高度 float addOffy = 0;
//對應標題下內容不為空白 例:以a開頭的內容有aaa,abc,abcd 則a標題下不為空白,addTitleCount加1 計數用 通過這個計算一共有
//多少項內不為空白 總共佔用多少header高度 最後一句中得22是我定義的一個viewforHeader的高度
float addTitleCount = 0;
//sectionTitles 是從a-z加上#之後的列表
//datasource 是對應我的沒個section中有幾項內容的資料 for (NSString * string in self.sectionTitles) { if ([string isEqualToString:title]) { break; } addOffy+=50*[[self.dataSource objectAtIndex:count] count]; if ([[self.dataSource objectAtIndex:count] count]!=0) { addTitleCount++; } count++; } return offY+22*(addTitleCount)+addOffy;}
iOS中Tableview右邊有字母檢索 點擊可以直接定位顯示的問題