你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UItableView可以編輯

iOS UItableView可以編輯

編輯:IOS開發綜合

//

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


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved