~~~
@implementation HMTAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
HMTRootViewController * rootRC = [[HMTRootViewController alloc]init];
UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rootRC];
self.window.rootViewController = rootNC;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
@interface HMTRootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
int i ;
}
@property (nonatomic,retain)NSMutableArray * iPhoneArrays;
@property (nonatomic,retain)UITableView * iPhoneTableView;
@end
#import "HMTRootViewController.h"
@interface HMTRootViewController ()
@end
@implementation HMTRootViewController
-(void)dealloc{
RELEASE_SAFELY(_allGroupArray);
RELEASE_SAFELY(_iPhoneTableView);
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_iPhoneArrays = [[NSMutableArray alloc]init];
i = 0;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 靈活運用數組
// 一個區的每一行數據(小數組)
NSArray * array1 = [NSArray arrayWithObjects:@"中國",@"日本",@"韓國", nil];
NSArray * array2 = [NSArray arrayWithObjects:@"德國",@"英國",@"法國",@"瑞士", nil];
NSArray * array3 = [NSArray arrayWithObjects:@"美國",@"俄羅斯",@"墨西哥", nil];
// 每一個區的數據(大數組)
self.iPhoneArrays = [NSMutableArray arrayWithObjects:array1,array2,array3, nil];
// 建立一個表單視圖
self.iPhoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 44)
style:UITableViewStyleGrouped];
_iPhoneTableView.separatorColor = [UIColor redColor];
_iPhoneTableView.delegate = self;
_iPhoneTableView.dataSource = self;
[self.view addSubview:_iPhoneTableView];
/*
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(didClickRightBarButtonItemAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
*/
// 用系統定義好的一個edit按鈕
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.editButtonItem.title = @"編輯";
// Do any additional setup after loading the view.
}
#pragma mark - 設置編輯環境,開啟/關閉
// 系統自帶按鈕
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
#pragma mark 推薦這種寫法
self.editButtonItem.title = editing?@"完成":@"編輯";
[_listTableView setEditing:editing animated:animated];
NSLOG_FUNCTION;
}
// 自定義編輯按鈕
- (void)didClickRightBarButtonItemAction:(UIBarButtonItem *)rightButton{
if (_isEditing) {
_isEditing = NO;
rightButton.title = @"編輯";
} else {
_isEditing = YES;
rightButton.title = @"完成";
}
// 熟悉這種寫法
//_isEditing = !_isEditing;
[_listTableView setEditing:_isEditing animated:YES];
}
#pragma mark - 數據設置
// 設置分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_iPhoneArrays count];
}
// 設置每個分區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[_iPhoneArrays objectAtIndex:section] count];
}
// cell的具體實現和設置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * celldentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:celldentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];
i++;NSLog(@" ------%i---------",i);
}
cell.textLabel.text = [[_iPhoneArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"亞洲";
} else if (section == 1){
return @"歐洲";
}else
return @"北美洲";
}
// cell被選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消選中后顯示的高亮狀態,也就是只在選中的那一刻出現高亮
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - 數據編輯Edit 刪除/添加
#pragma mark 詢問具體到某一區某一行能否被編輯(刪除/添加)
// 詢問能否被編輯 ----- 系統默認全部cell能編輯,這個方法能夠讓你設置具體到哪一行哪一列
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 返回編輯樣式,是添加還是刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// 第3個分區進行增加操作
if (indexPath.section == 2) {
return UITableViewCellEditingStyleInsert;
}
// 其余分區進行刪除操作
return UITableViewCellEditingStyleDelete;
}
#pragma mark 最后編輯操作(單純的設置這個方法,cell向左滑動會出現delete操作,短信.微信.QQ都是此操作)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// 判斷狀態
// 刪除數據
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 做判斷,如果
if ([deletaArray count] > 1) {
// 先刪除數據
[[_iPhoneArrays objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
// 刪除cell(指定行)
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section], nil]
withRowAnimation:UITableViewRowAnimationLeft];
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}else{
// 直接移除掉整個數組
[_iPhoneArrays removeObjectAtIndex:indexPath.section];
// 刪除整個分區
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
}
}
// 添加數據
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSString * text = @"天空之城";
// 先添加數據源
[[_iPhoneArrays objectAtIndex:indexPath.section] addObject:addCellText];
// 自定義要插入的行數(indexPath.row + ?(?>0) ?是幾就代表添加的數據會出現在+號下面的幾個位置處 ?為0就會出現在點擊加號那一行的上面)
NSIndexPath * selfIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:selfIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark - 數據編輯Edit 移動
#pragma mark 詢問具體到某一區某一行能否被編輯(移動)
// 詢問能否被移動(參數indexPath作用:指點哪些區哪些行能夠移動或者不能夠移動)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 設置能否被移動
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
// 一般,移動都是發生在同區之間,數據在不同區之間移動,那么數據一開始就應該不會編輯到一個區
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
// 允許移動(目的地)
return proposedDestinationIndexPath;
}else{
// 不允許移動(源始地)
return sourceIndexPath;
}
}
#pragma mark 完成最后移動,主要操作數據就行(注:移動并不是交換,移動看看就知道了,移動到一個位置,原先所有的cell會自動向下)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
// 取得要移動的數據
NSString * moveString = [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
// 移動到目的地
[[_iPhoneArrays objectAtIndex:sourceIndexPath.section] insertObject:moveString atIndex:destinationIndexPath.row];
// 刪除源始地的數據
[[_iPhoneArrays objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
~~~
- 前言
- UITableView詳解(UITableViewCell(一)重中之重)
- UITableView詳解(UITableViewCell(二) 自定義cell)
- UITableView詳解(UITableViewCell(三) cell根據文本長度來自動調整cell高度)
- UITableView詳解(UITableViewCell(四) 增加 刪除 移動)
- UITabBarController詳解(一)UITabBarController的介紹和設置(偷了點懶,直接用了ARC)
- UITabBarController詳解(二)UITabBarController的代理方法以及模態顯示
- UISearchBar詳解(一)基本屬性
- UISearchBar詳解(二)數據刷選類:NSPredicate
- UISearchDisplayController 的使用
- UINavigationController詳解(一)
- UINavigationController詳解(二)UINavigationBar(UIBarButtonItem)
- UINavigationController詳解(三)UIToolBar
- UINavigationController詳解(四)iOS7新特性
- UIScrollView控件詳解
- UISwitch用法-以及-自定義UISwitch控件
- UIAlertView用法
- UILabel 的常見屬性和方法:
- UIPickerView(滾動選擇控制器)
- UIActivityIndicatorView(活動指示器 ---------> 網絡卡后加載,畫面,圖像加載閃爍的圓圈)
- UIStepper
- UIImagePickerController--------圖片選取器
- UITextView
- UITabBarController詳解(三)自定義UITabBarController
- UIWebView基本介紹