ios 如何在cell中去掉_IOS之表视图单元格删除、移动及插入
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之表视图单元格删除、移动及插入相关推荐
- ios 如何在cell中去掉_iOS开发:关于 去除UITableViewCell复用机制 的几种方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击 ...
- ios 如何在cell中去掉_iOS-UITableViewCell三种常用编辑模式:删除,插入,移动
在开发中常常会有对TableViewCell编辑操作的需求,点击一下某个按钮,让cell处于编辑状态:还有向左滑动时,cell向右出现相应按钮可对cell进行相应的编辑.例如qq的好友列表,长按可以对 ...
- ios 如何在cell中去掉_经典问题:代码中如何去掉烦人的“!=nullquot;判空语句
问题 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗!如何避 ...
- java 合并和拆分单元格_如何在Microsoft Word中合并和拆分表和单元格
java 合并和拆分单元格 You can easily merge and split cells in Microsoft Word to make your tables more intere ...
- 如何在Excel中将多个单元格中的文本合并到一个单元格中
If you have a large worksheet in an Excel workbook in which you need to combine text from multiple c ...
- 如何在 Excel 中锁定受保护工作表的特定区域?
欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中锁定受保护工作表的特定区域. 只锁定受保护的工作表中的特定单元格和 ...
- 如何在Excel中使用数据透视表计算百分比变化
Pivot Tables are an amazing built-in reporting tool in Excel. While typically used to summarize data ...
- Python数据分析小技巧:如何在Pandas中实现数据透视表?
Python数据分析小技巧:如何在Pandas中实现数据透视表? 数据透视表是数据分析中非常有用的工具,可以帮助我们快速了解数据的结构.关联和趋势.在Pandas中,我们可以使用pivot_table ...
- excel单元格删除空格_在Excel数据输入单元格中防止空格
excel单元格删除空格 In Excel, you can use data validation to control (to some extent!) what users can enter ...
最新文章
- linux基础命令练习1
- iptables防***自动黑白名单脚本
- sql 赋值 null_巩固SQL - 窗口函数amp;变量amp;数据透视图
- 组件对象模型:COM
- Tcp_wrapper
- 谷歌开源 Pigweed,用于提升嵌入式开发效率
- mongo 让字段自增或自减
- 教你如何在Mac上搭建自己的服务器——Nginx
- 关于Meta标签中format-detection属性及含义
- java 请求webservice_JAVA调用WebService实例
- mysql默认密码树莓派_树莓派重置root密码
- 如何修改服务器的dns,如何修改服务器dns地址
- 雅虎新频道Buzz截图,Digg竞争者?
- aseprite手机版_Voxel像素画体素模型转拼豆图纸方法
- 太厉害了,阿里大牛把Java服务端做成一张超全的知识微缩地图
- 有时候可用 UIWebView 代替 UITextView,解决行间距问题
- Java 八大基本数据类型简述
- JS、JSP、JQuery区别
- Android小项目--Suzy计算器
- 两军交战在即,我却还在等妹纸脱衣服?!