美文网首页
iOS TableView交互 geekband

iOS TableView交互 geekband

作者: AAup | 来源:发表于2016-03-22 16:47 被阅读237次

TableView交互.选中
-响应

 -(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{
//提供该方法,就会打开左划编辑手势 .同时在这里面响应删除/新建操作
 }
 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//默认所有行均可以编辑
 }
 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//提供该方法,编辑状态下就会显示移动控件,在这里响应移动操作
 }

-----小案例-----
在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];

}

移动行

 //打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的
 - (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 {
}

搜索
关键代码:

@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 关联起来
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

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

再加入方法

 /*
  * 如果原 TableView 和 SearchDisplayController 中的 TableView 的      delete 指向同一个对象
  * 需要在回调中区分出当前是哪个 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;
 }
索引表格 索引优化 高亮与菜单

相关文章

网友评论

      本文标题:iOS TableView交互 geekband

      本文链接:https://www.haomeiwen.com/subject/ockllttx.html