#pragma mark -- 设置Cell移动模式
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
returnYES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
#pragma mark -- 编辑方法
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//能够编辑方法
returnYES;
}
//设置编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleInsert;
//return UITableViewCellEditingStyleDelete;
}
//编辑事件回调方法,此方法实现后,左划删除动画才会出现
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle==UITableViewCellEditingStyleDelete){
NSLog(@"删除");
}elseif(editingStyle==UITableViewCellEditingStyleInsert){
NSLog(@"添加");
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"输入添加内容"message:nilpreferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
[textField setPlaceholder:@"请输入姓名"];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
[textField setPlaceholder:@"请输入电话号码"];
}];
UIAlertAction *confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
//这里来写添加和显示数据的代码
//拿到
NSString *name = alert.textFields[0].text;
NSString *phone = alert.textFields[1].text;
//转换成对象
AddressListModel *model = [[AddressListModelalloc]initWithDictionary:@{@"name":name,@"phone":phone}];
[self.dataSourceinsertObject:model atIndex:indexPath.row+1];
//[tableView reloadData];
//创建一个indexPath
NSIndexPath *newIndexPath = [NSIndexPathindexPathForRow:indexPath.row+1inSection:indexPath.section];
[self.tableViewinsertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
UIAlertAction *cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
[alert addAction:confirm];
[alert addAction:cancel];
[selfpresentViewController:alertanimated:YEScompletion:nil];
}else{
NSLog(@"None");
}
}
//设置多个编辑标签
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {
NSLog(@"点击了%ld行的删除",indexPath.row);
[self.dataSourceremoveObjectAtIndex:indexPath.row];
//[self.tableView reloadData];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
UITableViewRowAction *markA = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"标记未读"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {
NSLog(@"点击了%ld行的标记",indexPath.row);
}];
return @[deleteAction,markA];
}
#pragma mark -- 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataSourcecount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier =@"ContactCell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell){
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cellIdentifier];
}
//获得对应的数据
AddressListModel *contact =self.dataSource[indexPath.row];
cell.textLabel.text = contact.name;
cell.detailTextLabel.text = contact.phone;
return cell;
}