table的编辑.删除
来源:互联网 发布:初学者吉他品牌知乎 编辑:程序博客网 时间:2024/06/06 01:53
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
//编辑步骤:
//1.让tableView处于编辑状态
//2.让cell处于编辑状态
//3.告诉cell,它的编辑样式
//4.提交编辑结果
@implementation AppDelegate
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
[_windowsetBackgroundColor:[UIColorwhiteColor]];
[_windowmakeKeyAndVisible];
[_windowrelease];
ViewController *main = [[ViewControlleralloc] init];
UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:main];
[_windowsetRootViewController:nav];
[mainrelease];
[navrelease];
return YES;
}
#import <UIKit/UIKit.h>
//枚举的位置
typedefNS_ENUM(NSInteger, EditType) {
EditTypeIsEditing,
EditTypeNormal
};
@interface ViewController :UIViewController
@property(nonatomic,retain)NSMutableArray *tableArray;
@property(nonatomic,retain)UITableView *table;
//设置属性的意义
@property(nonatomic,assign)NSInteger EditState;//0:代表正在被编辑,1:代表为未编辑
@end
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)dealloc
{
[_tableArray release];
[superdealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tableArray = [NSMutableArrayarray];
#warning 用plist快速完成初始化
// //取文件路径
// NSString *str = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];//文件名加后缀
// NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:str];//路径
// NSLog(@"array == %@",array);
// //把array里的元素一个一个取出来放到_tableArray里
// [_tableArray addObjectsFromArray:array];
NSMutableDictionary *dic1 = [NSMutableDictionarydictionary];
[dic1setObject:@"孙海东"forKey:@"name"];
NSMutableDictionary *dic2 = [NSMutableDictionarydictionary];
[dic2setObject:@"孙树海"forKey:@"name"];
[self.tableArrayaddObject:dic1];
[self.tableArrayaddObject:dic2];
//字符串数组的内存管理 ???
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
self.navigationController.navigationBar.translucent = NO;
[selfcreateSubviews];
self.EditState =EditTypeNormal;//默认
}
- (void)createSubviews
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(0,0,50,50);
[button setTitle:@"编辑"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *right = [[UIBarButtonItemalloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = right;
[rightrelease];
self.table = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height - 64) style:UITableViewStylePlain];
[_tablesetBackgroundColor:[UIColororangeColor]];
_table.rowHeight =100;
[self.viewaddSubview:_table];
[_tablerelease];
_table.delegate =self;
_table.dataSource =self;
}
//点击编辑 / 未编辑
- (void)buttonAction:(UIButton *)sender
{
NSLog(@"111");
//让tableView处于被编辑状态
if (self.EditState ==EditTypeNormal) {
[_tablesetEditing:YESanimated:YES];
self.EditState =EditTypeIsEditing;
//sender.title = @"完成";//用的系统的按钮
[sender setTitle:@"完成"forState:UIControlStateNormal];
}
else{
[_tablesetEditing:NOanimated:YES];
self.EditState =EditTypeNormal;
//sender.title = @"编辑";
[sender setTitle:@"编辑"forState:UIControlStateNormal];
}
}
#warning 返回值是UIView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *image = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"1.jpg"]];
return image;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIImageView *image = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"2.jpg"]];
return image;
}
#warning 返回值 大写字母数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *arr = [NSArrayarrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
return arr;
}
#warning 设置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 200;
}
#warning 让某个cell处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#warning 告诉cell被编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row ==0) {
returnUITableViewCellEditingStyleDelete;
}
else{
returnUITableViewCellEditingStyleInsert;
}
//return UITableViewCellEditingStyleNone;
}
#warning 滑动删除提交编辑结果 编辑状态改变完,提交的时候才走
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//界面和数据是分开的,要先删除数据,在删除界面
NSLog(@"555");
//一旦table的数据源(元素个数,结构等)发生了改变,则必须立刻刷新table的界面,即为每个section返回的行数变了
//[_tableArray removeObjectAtIndex:indexPath.row];
if (editingStyle ==UITableViewCellEditingStyleDelete) {
[_tableArrayremoveObjectAtIndex:indexPath.row];
//删除数据源(大数组里)的数据//弄清楚你想删的是哪个section里的行
} elseif(editingStyle ==UITableViewCellEditingStyleInsert) {
NSMutableDictionary *dic = [NSMutableDictionarydictionary];
[dicsetObject:@"nihao"forKey:@"name"];
[_tableArrayaddObject:dic];
}
//不停走重用池,只要数据源改变就要刷新数据
[tableViewreloadData];
}
#warning canMove
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#warning move
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//将要移动第几个
NSDictionary *dic = [[_tableArrayobjectAtIndex:sourceIndexPath.row]retain];
[_tableArrayremoveObjectAtIndex:sourceIndexPath.row];
[_tableArrayinsertObject:dic atIndex:destinationIndexPath.row];
[dicrelease];
[tableViewreloadData];
}
#warning 协议昨天学的 每个section有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tableArraycount];
}
#warning 只要上下滑动,就会走这条,走重用池,加载/回收数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentify =@"cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentify] autorelease];//要加autorelease
}
[cell setBackgroundColor:[UIColorwhiteColor]];
NSMutableDictionary *dic = [_tableArrayobjectAtIndex:indexPath.row];
cell.textLabel.text = [dicobjectForKey:@"name"];
return cell;
}
- table的编辑.删除
- table的编辑模式(多选,插入,删除)
- table的代理方法 删除cell可编辑状态
- 可以增加和删除行的table(可以编辑表格中内容)
- GridView 的编辑删除
- bootstrap table 双击可编辑,添加、删除行
- jquery:将table表变为可编辑状态,记录修改,新增或者删除的行。传入后台controller中
- SWT的Table表格可编辑单元格
- 29.table view的编辑模式
- 简单粗暴的table行编辑保存
- Jquery 实现可编辑的table
- 双击table的td进行编辑
- 基于jsf的批量编辑,可编辑table
- GridView的编辑、新增、删除
- GridView的编辑删除代码
- 文件的创建,删除,编辑
- repeater的编辑,更新,删除
- repeater的编辑,更新,删除
- UIImageView总结
- UVa10763 - Foreign Exchange
- CPU缓存刷新的误解
- android中将数据写入手机内存和sdcard中的文件
- 使用python中的Matplotlib绘图示例
- table的编辑.删除
- java unicode
- UIKit框架-高级控件:4.UIDatePickerView的基本认识
- UILabel的各种属性与方法的使用
- LVS+keepalived搭建负载均衡学习手札(一)
- SVN图标不见了?
- LayoutInflater作用及使用
- LVS+keepalived搭建负载均衡学习手札(二)
- JsonParser