iOS UItableView可以编辑
来源:互联网 发布:怎么制作淘宝客小程序 编辑:程序博客网 时间:2024/05/23 16:19
//
// RootViewController.m
// TableViewEdit
//
// Created by Duba on 15-5-07.
// Copyright (c) 2015年 Dubai. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
{
NSMutableArray *_dataArray;
UITableView *_tableView;
NSMutableArray *_arr1;
}
@end
@implementation RootViewController
- (void)dealloc
{
[_dataArray release];
[_tableView release];
[superdealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
//亚洲
_arr1 = [NSMutableArrayarrayWithObjects:@"中国",@"新加坡",@"蒙古",nil];
//欧洲
NSMutableArray *arr2 = [NSMutableArrayarrayWithObjects:@"美国",@"加拿大",@"巴西",nil];
//欧洲
NSMutableArray *arr3 = [NSMutableArrayarrayWithObjects:@"英国",@"法国",@"德国",nil];
//把三个州的小数组添加到大数组里
_dataArray = [[NSMutableArrayalloc]initWithObjects:_arr1,arr2,arr3,nil];
// //加一个按钮
// UIBarButtonItem *edite = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(didClickEditButtonitem:)];
// self.navigationItem.rightBarButtonItem = edite;
// [edite release];
//使用controller自带的编辑buttonitem,直接用不用创建对象
self.navigationItem.rightBarButtonItem =self.editButtonItem;//自带一个属性
_tableView = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:(UITableViewStyleGrouped)];
[self.viewaddSubview:_tableView];
_tableView.dataSource =self;
_tableView.delegate =self;
_tableView.separatorColor = [UIColorgreenColor];
//self.editButtonItem.title = @"编辑";
}
//controller的重写方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editing animated:animated];
//NSLog(@"%d",editing);
[_tableViewsetEditing:editing animated:animated];
self.editButtonItem.title = editing?@"完成":@"编辑";
}
////按钮响应,进入编辑状态
//- (void)didClickEditButtonitem:(UIBarButtonItem *)buttonitem
//{
//
// //tableView进入编辑状态
// if ([buttonitem.title isEqualToString:@"编辑"]) {
// [_tableView setEditing:YES animated:YES];
// buttonitem.title = @"完成";
// }else{
//
// [_tableView setEditing:NO animated:YES];
// buttonitem.title = @"编辑";
//
// }
// //[_tableView setEditing:YES animated:YES];
//
//
//
//}
//设置 tableView哪行可以编辑,默认是YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//设置tableViewCell编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;//默认是删除
// return UITableViewCellEditingStyleInsert;//插入
}
//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//删数据,
// NSMutableArray *onegroup = _dataArray[indexPath.section];
// [onegroup removeObjectAtIndex:indexPath.row];
////
// NSArray *aeeay = [NSArray arrayWithObjects:indexPath, nil];
// NSArray *aeeay = @[indexPath];//同下面的同等
//
//删除(可以是n行)
if (editingStyle ==UITableViewCellEditingStyleDelete) {
NSMutableArray *onegroup = _dataArray[indexPath.section];
//如果分区中只剩一行,就删整个分区
if (onegroup.count ==1) {
[_dataArrayremoveObject:onegroup];//删除某个分区的数据
//tableview删除section
NSIndexSet *set = [NSIndexSetindexSetWithIndex:indexPath.section];
[_tableViewdeleteSections:set withRowAnimation:(UITableViewRowAnimationLeft)];
}else {
//删除某行数据
[onegroupremoveObjectAtIndex:indexPath.row];
//NSArray *arr = [NSArray arrayWithObjects:indexPath, nil];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationAutomatic)];
}
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];//crash.只删除行.没有删除数据
// //三条数据显示在两行上(得先删数据)
}
//添加
if (editingStyle ==UITableViewCellEditingStyleInsert) {
NSMutableArray *onegroup = _dataArray[indexPath.section];
if (onegroup.count !=0) {
[tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationBottom)];
//_arr1 = [[NSMutableArray alloc] initWithObjects:@"北京", nil];
[onegroupaddObject:@"北京"];
_dataArray = [[NSMutableArrayalloc] initWithObjects:onegroup,nil];
[_tableViewreloadData];
NSLog(@"添加不上");
}
}
}
//2设置tableview能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//2完成移动(数据自己来处理移动)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//处理数据
//找获取对应的分组
NSMutableArray *onegroup = _dataArray[sourceIndexPath.section];
//先将药膳粗的元素retain(防止删除时出问题)
NSString *name = [onegroup[sourceIndexPath.row]retain];//找元素jia 1
//删除元素
[onegroupremoveObject:name];
//插入到指定位置
[onegroupinsertObject:name atIndex:destinationIndexPath.row];
[namerelease];
}
//设置cell能否移动到计划的目的地propose(代理)
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//如果移动的范围是在一个同一个区section可以移动
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;//返回目的地
}
return sourceIndexPath;//不让跨区移动
//return proposedDestinationIndexPath;//可以移动
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
//return 3;
return [_dataArraycount];
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//3个分区分别为 10 15 20 行
// if (section == 0) {
// return 10;
// }
// if (section == 1) {
// return 15;
// }
//return 10;
NSArray *array = _dataArray[section];
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *identifier = @"cell";
//1从队列中取出cell对象
static NSString *identifier =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//2如果没有就重新创建
if (cell == nil) {
cell= [[[UITableViewCellalloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:identifier] autorelease];
}
// //3根据section,row,对cell进行设置,显示相应的内容.
// cell.textLabel.text = [NSString stringWithFormat:@" 分区:%ld 行:%ld",indexPath.section,indexPath.row];
// //(此时的cell可能是新创建按的,可能是从队列中取来的)
// //4将设置完的cell对象返回给tabelView
NSArray *array = _dataArray[indexPath.section];
cell.textLabel.text = array[indexPath.row];
return cell;
}
////给每个分区 设置头部标题
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// //分别显示 1 2 3
// return [NSString stringWithFormat:@"%ld",section+1];//索引是从0开始的
//
//
//}
//所有
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//
// return @[@"1",@"2",@"3"];
//
//}
////设置行高
//- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.section == 0) {
// return 80;
// }
// if (indexPath.section == 1) {
// return 150;
// }
// return 60;
//
//}
//
//检测cell被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" 分区:%ld行数:%ld",indexPath.section ,indexPath.row);
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- iOS UItableView可以编辑
- IOS-UITableView编辑模式
- IOS-UITableView编辑模式示例
- iOS编程-------UITableViewEdit_move -----> UITableView 编辑
- iOS UITableView的编辑方法
- UITableView编辑模式可以点击Cell
- iOS中UITableView的编辑模式
- iOS UITableView的刷新和编辑
- 编辑UITableView
- UITableView编辑
- UITableView编辑
- UITableView编辑
- UITableView编辑
- UITableVIew 编辑
- UITableView 编辑
- 【UITableView】UITableView 编辑
- iOS之UITableView的使用——编辑表格
- iOS Programming 学习笔记 - 04 UITableView编辑模式
- c语言全局变量const static int
- nil/Nil/NULL/NSNull
- Length of Last Word
- Android手工测试的代码覆盖率
- 手把手教你做智能手环
- iOS UItableView可以编辑
- Android一点 ListView显示多种布局
- 堆排序
- cookie的path值的默认规则
- ios uiwebview 详解
- 【Servlet】Servlet3.0与纯javascript通过Ajax交互
- PHP 之局部变量与全局变量的交错(一)嵌套for循环中的变量
- offline页面开发常用方法及页面控件验证
- final的使用