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

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

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

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

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

2.移动单元格

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

  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. }
//完成移动的触发事件,不添加该方法不实现移动功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];
[dataArr removeObjectAtIndex:sourceIndexPath.row];
[dataArr insertObject:item atIndex:destinationIndexPath.row];
}

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

  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. }
- (IBAction)addistItem:(UIBarButtonItem *)sender {
AppUtils *appUtils = [AppUtils alloc];
//需要先初始化一个UIAlertView
UIAlertView *alert = [UIAlertView alloc];
[appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{
//得到输入框
UITextField *textField=[alert textFieldAtIndex:0];
// 不要写成NSMutableDictionary *newItem = [NSDictionary dictionary];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setObject:textField.text forKey:@"itemName"];
[newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];
[dataArr addObject:newItem];
[self.tableView reloadData];
})];
}

4.附上·AppUtils类

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

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

上一篇:关于 Mybatis 的Invalid bound statement (not found):错误


下一篇:自己写的POIUtil,主要解决从不同的HSSFWorkbook复制sheet以及根据单元格插入图片等