iOS中Tableview右邊有字母檢索 點擊可以直接定位顯示的問題

來源:互聯網
上載者:User

標籤: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右邊有字母檢索 點擊可以直接定位顯示的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.