#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;
}