IOS之表视图单元格删除、移动及插入

 

1.实现单元格的删除,实现效果如下

   

Cpp代码   收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //设置导航栏  
  5.      self.editButtonItem.title = @"编辑";  
  6.     self.navigation.rightBarButtonItem = self.editButtonItem;  
  7.     [self initTableViewData];  
  8.     // Do any additional setup after loading the view.  
  9. }  
  10.   
  11. - (void)didReceiveMemoryWarning  
  12. {  
  13.     [super didReceiveMemoryWarning];  
  14.     // Dispose of any resources that can be recreated.  
  15. }  
  16.   
  17. -(void)initTableViewData{  
  18.     NSBundle *bundle = [NSBundle mainBundle];  
  19.     NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];  
  20.     dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];  
  21. }  
  22.   
  23. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  24. {  
  25.     return [dataArr count];  
  26. }  
  27.   
  28. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  29. {  
  30.     static NSString *CellIdentifier = @"tableCell";  
  31.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  32.       
  33.     NSUInteger row = [indexPath row];  
  34.     NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  35.     cell.textLabel.text =  [rowDict objectForKey:@"itemName"];  
  36.     NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);  
  37.       
  38.     NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];  
  39.     cell.imageView.image = [UIImage imageNamed:imagePath];  
  40.     NSLog(@"cell.image.image  =  %@",imagePath);  
  41.       
  42.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  43.       
  44.     return cell;  
  45. }  
  46.   
  47. //选中Cell响应事件  
  48. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  49.     [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
  50.     NSUInteger row = [indexPath row];  
  51.     NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  52.     NSString *userName =  [rowDict objectForKey:@"itemName"];  
  53.     NSLog(@"userName=%@",userName);  
  54. }  
  55.   
  56. //返回编辑状态的style  
  57. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  
  58.            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  59. {  
  60.     //UITableViewCellEditingStyleInsert  
  61. //    return UITableViewCellEditingStyleNone;  
  62.     return UITableViewCellEditingStyleDelete;  
  63. }  
  64. //完成编辑的触发事件  
  65. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  66. forRowAtIndexPath:(NSIndexPath *)indexPath  
  67. {  
  68.     if (editingStyle == UITableViewCellEditingStyleDelete)  
  69.     {  
  70.         [dataArr removeObjectAtIndex: indexPath.row];  
  71.         //        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  72.         //                         withRowAnimation:UITableViewRowAnimationFade];  
  73.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  74.                          withRowAnimation:UITableViewRowAnimationFade];  
  75.         [tableView reloadData];  
  76.     }   
  77. }  
  78. //UIViewController生命周期方法,用于响应视图编辑状态变化  
  79. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {  
  80.     [super setEditing:editing animated:animated];  
  81.       
  82.     [self.tableView setEditing:editing animated:YES];  
  83.     if (self.editing) {  
  84.      self.editButtonItem.title = @"完成";  
  85.     } else {  
  86.         self.editButtonItem.title = @"编辑";  
  87.     }  
  88. }  
  89. @end  

 2.移动单元格

 

Cpp代码   收藏代码
  1. //完成移动的触发事件,不添加该方法不实现移动功能  
  2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath  
  3.       toIndexPath:(NSIndexPath *)destinationIndexPath  
  4. {  
  5.     NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];  
  6.     [dataArr removeObjectAtIndex:sourceIndexPath.row];  
  7.     [dataArr insertObject:item atIndex:destinationIndexPath.row];  
  8. }  

 3.添加单元格。下面是自定义触发事件,即单击左下角的add按钮

Cpp代码   收藏代码
  1. - (IBAction)addistItem:(UIBarButtonItem *)sender {  
  2.     AppUtils *appUtils = [AppUtils alloc];  
  3.     //需要先初始化一个UIAlertView  
  4.     UIAlertView *alert = [UIAlertView alloc];  
  5.     [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{  
  6.         //得到输入框  
  7.         UITextField *textField=[alert textFieldAtIndex:0];  
  8. //        不要写成NSMutableDictionary *newItem = [NSDictionary dictionary];  
  9.         NSMutableDictionary *newItem = [NSMutableDictionary dictionary];  
  10.         [newItem setObject:textField.text forKey:@"itemName"];  
  11.         [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];  
  12.         [dataArr addObject:newItem];  
  13.         [self.tableView reloadData];  
  14.     })];  
  15. }  

 4.附上·AppUtils类

Cpp代码   收藏代码
  1. #import "AppUtils.h"  
  2. #include "RIButtonItem.h"  
  3. #include "UIAlertView+Blocks.h"  
  4.   
  5. @implementation AppUtils  
  6.   
  7. //弹出警告框,并实现警告框按钮的触发事件  
  8. - (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{  
  9.     RIButtonItem* cancelItem = [RIButtonItem item];  
  10.     cancelItem.label = @"No";  
  11.     cancelItem.action = ^  
  12.     {  
  13.         //为NO时的处理  
  14.         UITextField *tf=[alert textFieldAtIndex:0];  
  15.         NSLog(@"UITextField=%@",tf.text);  
  16.     };  
  17.       
  18.     RIButtonItem* confirmItem = [RIButtonItem item];  
  19.     confirmItem.label = @"Yes";  
  20. //    confirmItem.action = action;  
  21.     alert = [alert initWithTitle:title  
  22.                                                     message:message  
  23.                                            cancelButtonItem:cancelItem  
  24.                                            otherButtonItems:confirmItem, nil];  
  25.       
  26.     alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
  27.       
  28.     confirmItem.action = action;  
  29.     [alert show];  
  30. }  
  31. @end  

 

转载于:https://www.cnblogs.com/lovewx/p/3860858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中,要实现点击单元格切换视图的功能,有以下几个步骤: 1. 创建一个 UITableView,并设置其代理和数据源。 2. 在代理方法 `tableView(_:didSelectRowAt:)` 中,监听单元格的点击事件。 3. 在点击事件中,获取点击的单元格索引,然后根据需要进行视图切换。 下面是一个简单的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 设置tableView的frame和代理 tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self // 注册单元格 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // 添加tableView视图中 view.addSubview(tableView) } // 返回单元格数量 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } // 创建和配置单元格 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row + 1)" return cell } // 单元格点击事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取点击的单元格索引 let selectedRow = indexPath.row // 根据需要进行视图切换 switch selectedRow { case 0: // 切换到视图1 let viewController1 = ViewController1() navigationController?.pushViewController(viewController1, animated: true) case 1: // 切换到视图2 let viewController2 = ViewController2() navigationController?.pushViewController(viewController2, animated: true) // 其他切换逻辑... default: break } // 取消选中效果 tableView.deselectRow(at: indexPath, animated: true) } } ``` 在上述示例中,我们创建了一个包含 5 个单元格的 UITableView,并通过点击单元格来切换到不同的视图。你可以根据自己的需求,定义不同的视图,并在 `didSelectRowAt` 方法中进行切换逻辑的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值