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)化
高亮與菜單