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
- (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];
- }
//完成移动的触发事件,不添加该方法不实现移动功能
- (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];
- })];
- }
- (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
#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之表视图单元格删除、移动及插入的更多相关文章
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- iOS集合视图单元格高亮和选中的区别
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- Excel2007VBA数组和工作表及单元格的引用
动态数组使用: https://zhidao.baidu.com/question/1432222709706721499.html 使用Redim动态数组即可. 1 2 3 4 5 6 7 8 Su ...
- iOS中表视图单元格事件用nib和storyboard的两种写法总结
从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...
- IOS开发-表视图LV3导航控制器
学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...
- ABAP 动态内表添加单元格颜色字段
*动态内表alv显示时要求某些单元格显示颜色 *wa_fldcat-datatype不能添加LVC_T_SCOL类型,在创建好内表之后,再添加颜色列. DATA: wa_fldcat TYPE lvc ...
- iOS开发-表视图的使用
// // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jie ...
- easyui DataGrid表体单元格跨列rowspan
最近做项目用到了jquery easyui,其中一组DataGrid做的报表是给客户大领导看的,客户要求报表样式跟他们原有系统的一模一样(如下图1). DataGrid样式好调,只是城市名称单元格跨行 ...
- 【VBA】单元格插入图片,单元格删除图片
封装函数: Sub 插入产品形象(strRange As String, datebaseTu As String) Dim strJpg As String strJpg = datebaseTu ...
随机推荐
- http协议状态码对照表
1**:请求收到,继续处理 2**:操作成功收到,分析.接受 3**:完成此请求必须进一步处理 4**:请求包含一个错误语法或不能完成 5**:服务器执行一个完全有效请求失败 100——客户必须继续发 ...
- 二模09day1解题报告
T1.词编码(word) 给出一些原长为n的01串经过变化后的串求原串.原串的特点是:各个1的位置号和%(n+1)==0 变法(只取其一): 改一个0为1 删一个 加一个 不变. 其中2优先考虑位置靠 ...
- oracle:jdbcTest
JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...
- linux 下wifi 功能的实现
一.嵌入式linux四件套配置 1.网卡选型的依据: a.网卡不能乱选,否则没戏.网卡的依据是,kernel的menuconfig -> device drivers -> network ...
- ADO.NET中ExcuteNonQuery获取存储过程Return返回值
/// <summary> /// 获取当月用户已投票数量 /// </summary> /// <param name="userId">用户 ...
- 设置field的背景颜色以及对stylesheet的理解
今天遇到一个需求:在做页面输入验证的时候,如果用户没有输入某个项,那么这个项显示为红色,一直没头绪,也找peoplebook,发现field有一个style的方法,后来又在谷歌上找,终于找到了方法: ...
- mutex 简单介绍
“mutex”是术语“互相排斥(mutually exclusive)”的简写形式,也就是互斥量. 当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源.M ...
- 用,隔开sql临时表
IF OBJECT_ID('[kkd].[proc_kkd_GetAutoExamineBid]') IS NOT NULL BEGIN DROP PROC [kkd].[proc_kkd_GetAu ...
- 什么是Ajax无刷新技术?
浏览器实例化一个Ajax对象,这个对象发送一个HTTP请求,并且携带一定的参数,传输到后台.后台服务器接收这些参数,同时过滤一下传过来的参数,做出逻辑判断.如果需要数据库操作参与,就要取出数据,格式化 ...
- “Unable to resolve target 'android-9'”的问题
SDK版本问题! 如果导入时,eclipse的console中提示信息:“Unable to resolve target 'android-9'” 可能的问题就是你安装的SDK版本高于项目的版本!( ...