TableView索引準(zhǔn)確定位

在做項(xiàng)目的過程中,我遇到這樣一個(gè)問題,就是本身的tableview 調(diào)用

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

方法的時(shí)候,最后幾個(gè)位置點(diǎn)擊后不能準(zhǔn)確定位,比如說“#” 不管我如何點(diǎn)擊“#”都無法把其對(duì)應(yīng)的列表項(xiàng)顯示出來,所以我自己在

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

方法中重寫了一些方法 代碼如下

- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
  //1.獲取當(dāng)前index的section的original的y
  //2.用tableview.contentsize.height減去y,得到lefty
  //3.如果lefty>=tableview.frame.size.height 滾動(dòng)lefty個(gè)單位
  //4.如果lefty<tableview.frame.size.height 滾動(dòng)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;
}

//22是斷頭高度,50是每行高度,100是上面無索引部分附加的高度

-(float)getYOffSet:(NSInteger)index title:(NSString *)title{
//這里的offy = 100 是我在這個(gè)tableview最上面加了兩個(gè)section 不在這個(gè)計(jì)算之內(nèi) 顯示了別的東西 對(duì)于不需要添加特別提示等//顯示,可以設(shè)置為0
float offY = 100;
    int count = 0;
//對(duì)應(yīng)的所有內(nèi)容的高度
    float addOffy = 0;
//對(duì)應(yīng)標(biāo)題下內(nèi)容不為空 例:以a開頭的內(nèi)容有aaa,abc,abcd 則a標(biāo)題下不為空,addTitleCount加1 計(jì)數(shù)用 通過這個(gè)計(jì)算一共有
//多少項(xiàng)內(nèi)不為空 總共占用多少header高度 最后一句中得22是我定義的一個(gè)viewforHeader的高度
float addTitleCount = 0;
//sectionTitles 是從a-z加上#之后的列表
//datasource 是對(duì)應(yīng)我的沒個(gè)section中有幾項(xiàng)內(nèi)容的數(shù)據(jù)
  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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容