iOS TableView交互 geekband

TableView交互.選中
-響應(yīng)

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{  
 }
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
 }

-用代碼選中

 -(void)deselectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated {
 }
 -(void)selectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition{    
 }

scrollPosition.類型


Snip20160322_12.png

-讀取

 NSIndexPath *indexPathForSelectedRow
 NSArry<NSIndexPath *>ndexPathForSelectedRow

代碼控制表格滾動

 -scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated{ 
 }
 -scrollToNearestSeletcedRowAtScrollPosition:(UITableViewScrollPosition) animated:(BOOL)animated{    
 }

編輯模式

 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//提供改方法則默認為Delete
//UITableViewCellEditingStyleNone;
//UITableViewCellEditingStyleDelete;
//UITableViewCellEditingStyleInsert;
 }
 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//提供該方法,就會打開左劃編輯手勢 .同時在這里面響應(yīng)刪除/新建操作
 }
 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//默認所有行均可以編輯
 }
 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//提供該方法,編輯狀態(tài)下就會顯示移動控件,在這里響應(yīng)移動操作
 }

-----小案例-----
在viewcontroller.m加入
- (void)viewDidLoad {
[super viewDidLoad];

self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.editButtonItem setTitle:@"顯示按鈕"];

}

Snip20160322_16.png

點擊打鉤 就用上文的代碼選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];
if (oneCell.accessoryType == UITableViewCellAccessoryNone) {
oneCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else
oneCell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

移動行

 //打開編輯模式后款熬,默認情況下每行左邊會出現(xiàn)紅的刪除按鈕隐砸,這個方法就是關(guān)閉這些按鈕的
 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
       editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
 }
 //這個方法用來告訴表格 這一行是否可以移動
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
 }
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
}

搜索
關(guān)鍵代碼:

@interface MainViewController : UITableViewController{
NSArray *data;
NSArray *filterData;
UISearchDisplayController *searchDisplayController;
}

下一個

 - (void)viewDidLoad{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width
                                                                       , 44)];
searchBar.placeholder = @"搜索";

// 添加 searchbar 到 headerview
self.tableView.tableHeaderView = searchBar;

// 用 searchbar 初始化 SearchDisplayController
// 并把 searchDisplayController 和當前 controller 關(guān)聯(lián)起來
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

// searchResultsDataSource 就是 UITableViewDataSource
searchDisplayController.searchResultsDataSource = self;
// searchResultsDelegate 就是 UITableViewDelegate
searchDisplayController.searchResultsDelegate = self;
}

再加入方法

 /*
  * 如果原 TableView 和 SearchDisplayController 中的 TableView 的      delete 指向同一個對象
  * 需要在回調(diào)中區(qū)分出當前是哪個 TableView
  */
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
if (tableView == self.tableView) {
    return data.count;
}else{
    // 謂詞搜索
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text];
    filterData =  [[NSArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];
    return filterData.count;
}
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *cellId = @"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}

if (tableView == self.tableView) {
    cell.textLabel.text = data[indexPath.row];
}else{
    cell.textLabel.text = filterData[indexPath.row];
}
return cell;
 }
索引表格
索引優(yōu)化
高亮與菜單
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酥诽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子创肥,更是在濱河造成了極大的恐慌,老刑警劉巖级遭,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件龙巨,死亡現(xiàn)場離奇詭異,居然都是意外死亡带到,警方通過查閱死者的電腦和手機昧碉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來揽惹,“玉大人被饿,你說我怎么就攤上這事√虏” “怎么了锹漱?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長慕嚷。 經(jīng)常有香客問我,道長毕泌,這世上最難降的妖魔是什么喝检? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮撼泛,結(jié)果婚禮上挠说,老公的妹妹穿的比我還像新娘。我一直安慰自己愿题,他們只是感情好损俭,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著潘酗,像睡著了一般杆兵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上仔夺,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天琐脏,我揣著相機與錄音,去河邊找鬼。 笑死日裙,一個胖子當著我的面吹牛吹艇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播昂拂,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼受神,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了格侯?” 一聲冷哼從身側(cè)響起鼻听,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎养交,沒想到半個月后精算,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡碎连,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年灰羽,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鱼辙。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡廉嚼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出倒戏,到底是詐尸還是另有隱情怠噪,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布杜跷,位于F島的核電站傍念,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏葛闷。R本人自食惡果不足惜憋槐,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望淑趾。 院中可真熱鬧阳仔,春花似錦、人聲如沸扣泊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽延蟹。三九已至评矩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間等孵,已是汗流浹背稚照。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人果录。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓上枕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親弱恒。 傳聞我的和親對象是個殘疾皇子辨萍,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實...
    香橙柚子閱讀 23,861評論 8 183
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,140評論 30 470
  • 二十年前始牽手 中秋月圓吻定情 以為今生遇知己 執(zhí)子之手到白頭 萬般矛盾且不論 吵吵鬧鬧意堅決 渾渾噩噩終蹉跎 今...
    娥兒閱讀 98評論 0 0
  • 駱承烈: 曲阜師范大學(xué)教授返弹、中國孔子文化傳播促進會副會長锈玉、《中華人民共和國大典》顧問、國務(wù)院幼兒園首席顧問义起、本課題...
    秦東魁閱讀 238評論 0 0
  • 2017年2月25日拉背,公司一紙調(diào)令,我從客服部經(jīng)理轉(zhuǎn)型產(chǎn)品部經(jīng)理默终∫喂祝客服經(jīng)理的崗位已經(jīng)做了8年,一切了如指掌齐蔽,對個人...
    史小魯閱讀 1,137評論 0 1