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

- (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.移动单元格

//完成移动的触发事件,不添加该方法不实现移动功能

- (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按钮

- (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类

#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 如何在cell中去掉_IOS之表视图单元格删除、移动及插入相关推荐

  1. ios 如何在cell中去掉_iOS开发:关于 去除UITableViewCell复用机制 的几种方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击 ...

  2. ios 如何在cell中去掉_iOS-UITableViewCell三种常用编辑模式:删除,插入,移动

    在开发中常常会有对TableViewCell编辑操作的需求,点击一下某个按钮,让cell处于编辑状态:还有向左滑动时,cell向右出现相应按钮可对cell进行相应的编辑.例如qq的好友列表,长按可以对 ...

  3. ios 如何在cell中去掉_经典问题:代码中如何去掉烦人的“!=nullquot;判空语句

    问题 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗!如何避 ...

  4. java 合并和拆分单元格_如何在Microsoft Word中合并和拆分表和单元格

    java 合并和拆分单元格 You can easily merge and split cells in Microsoft Word to make your tables more intere ...

  5. 如何在Excel中将多个单元格中的文本合并到一个单元格中

    If you have a large worksheet in an Excel workbook in which you need to combine text from multiple c ...

  6. 如何在 Excel 中锁定受保护工作表的特定区域?

    欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中锁定受保护工作表的特定区域. 只锁定受保护的工作表中的特定单元格和 ...

  7. 如何在Excel中使用数据透视表计算百分比变化

    Pivot Tables are an amazing built-in reporting tool in Excel. While typically used to summarize data ...

  8. Python数据分析小技巧:如何在Pandas中实现数据透视表?

    Python数据分析小技巧:如何在Pandas中实现数据透视表? 数据透视表是数据分析中非常有用的工具,可以帮助我们快速了解数据的结构.关联和趋势.在Pandas中,我们可以使用pivot_table ...

  9. excel单元格删除空格_在Excel数据输入单元格中防止空格

    excel单元格删除空格 In Excel, you can use data validation to control (to some extent!) what users can enter ...

最新文章

  1. linux基础命令练习1
  2. iptables防***自动黑白名单脚本
  3. sql 赋值 null_巩固SQL - 窗口函数amp;变量amp;数据透视图
  4. 组件对象模型:COM
  5. Tcp_wrapper
  6. 谷歌开源 Pigweed,用于提升嵌入式开发效率
  7. mongo 让字段自增或自减
  8. 教你如何在Mac上搭建自己的服务器——Nginx
  9. 关于Meta标签中format-detection属性及含义
  10. java 请求webservice_JAVA调用WebService实例
  11. mysql默认密码树莓派_树莓派重置root密码
  12. 如何修改服务器的dns,如何修改服务器dns地址
  13. 雅虎新频道Buzz截图,Digg竞争者?
  14. aseprite手机版_Voxel像素画体素模型转拼豆图纸方法
  15. 太厉害了,阿里大牛把Java服务端做成一张超全的知识微缩地图
  16. 有时候可用 UIWebView 代替 UITextView,解决行间距问题
  17. Java 八大基本数据类型简述
  18. JS、JSP、JQuery区别
  19. Android小项目--Suzy计算器
  20. 两军交战在即,我却还在等妹纸脱衣服?!

热门文章

  1. Pycharm中一些不为人知的技巧pycharm技巧
  2. LeetCode简单题之二叉树中第二小的节点
  3. LeetCode简单题之检查某单词是否等于两单词之和
  4. ResNet网络的训练和预测
  5. TorchScript神经网络集成技术
  6. 客快物流大数据项目(十一):Docker应用部署
  7. CentOS7下安装nvm
  8. php 正则中文匹配
  9. [JS] 闭包与内存泄漏
  10. [JavaScript] 日期时间戳的使用与计算