您的位置:首頁技術文章
文章詳情頁

iOS tableView右側索引視圖狀態獲取的方法實例

瀏覽:9日期:2022-09-16 15:41:09
前言

在iPhone和其他iOS的很多程序中都會看到Table View的出現,除了一般的表格資料展示之外,設置的屬性資料往往也用到Table View,Table View主要分為以下兩種:

iOS tableView右側索引視圖狀態獲取的方法實例

Plain:這是普通的列表風格 Grouped :這是分塊風格。

本文介紹的是iOS tableView右側索引視圖狀態獲取的相關內容,下面來看正文

需求

一圖勝千言!

iOS tableView右側索引視圖狀態獲取的方法實例

在觸摸右側索引欄時

需要展示對應的組號名稱

手指離開時消失

實現

通過Xcode查看視圖層次結構

右側索引視圖的繼承關系

是這樣的:

UITableViewIndexUIControlUIViewUIResponderNSObject

很顯然UITableViewIndex 是私有的

但是UIControl 是公開的

于是

通過遍歷tableView的子視圖來獲取它

- (void)reloadData{ [_tableView reloadData];for (UIView *view in _tableView.subviews) {if ([view isKindOfClass:[UIControl class]]) { _indexControl = (UIControl *)view;} }}

接著

在代理方法里面

處理相關邏輯就OK了

/// 點擊右側索引時的代理方法- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ _sectionLabel.text = title; // 顯示 組號標題 的Label if (_sectionLabel.hidden) { // 隱藏了,顯示它_sectionLabel.hidden = NO; }// 延遲 1 秒隱藏 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// isTracking: A Boolean value indicating whether the control is currently tracking touch events.if (!_indexControl.isTracking) { // 沒有觸摸時,隱藏它 _sectionLabel.hidden = YES;} });return index;}總結

到此這篇關于iOS tableView右側索引視圖狀態獲取的文章就介紹到這了,更多相關iOS tableView索引視圖狀態內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: IOS
相關文章:
国产综合久久一区二区三区