iOS UItableView可以編輯

來源:互聯網
上載者:User

iOS UItableView可以編輯

//

// 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];

[super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

 

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

 

//亞洲

_arr1 = [NSMutableArray arrayWithObjects:@"中國",@"新加坡",@"蒙古", nil];

//歐洲

NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"美國",@"加拿大",@"巴西", nil];

 

//歐洲

NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"英國",@"法國",@"德國", nil];

 

//把三個州的小數組添加到大數組裡

_dataArray = [[NSMutableArray alloc]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 = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];

[self.view addSubview:_tableView];

_tableView.dataSource = self;

_tableView.delegate = self;

_tableView.separatorColor = [UIColor greenColor];

//self.editButtonItem.title = @"編輯";

 

}

 

//controller的重寫方法

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

[super setEditing:editing animated:animated];

//NSLog(@"%d",editing);

[_tableView setEditing: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

{

return UITableViewCellEditingStyleDelete;//預設是刪除

// 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) {

[_dataArray removeObject:onegroup];//刪除某個分區的資料

 

 

//tableview刪除section

NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];

[_tableView deleteSections:set withRowAnimation:(UITableViewRowAnimationLeft)];

 

 

}else {

//刪除某行資料

[onegroup removeObjectAtIndex: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];

[onegroup addObject:@"北京"];

_dataArray = [[NSMutableArray alloc] initWithObjects:onegroup, nil];

[_tableView reloadData];

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

//刪除元素

[onegroup removeObject:name];

//插入到指定位置

[onegroup insertObject:name atIndex:destinationIndexPath.row];

[name release];

 

 

}

 

//設定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 [_dataArray count];

 

 

}

 

- (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= [[[UITableViewCell alloc] 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

{

[super didReceiveMemoryWarning];

// 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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.