// 頭文件 // // GroupTableViewController.h // UITableViewGroup // // Created by LiZe on 13-9-5. // Copyright (c) 2013年 BlackCode. All rights reserved. // #import #import "DetailViewController.h" @interface GroupTableViewController : UIViewController<</span>UITableViewDelegate, UITableViewDataSource> @property (nonatomic, retain) UITableView *groupTableView; @end
//
// GroupTableViewController.m
// UITableViewGroup
//
// Created by LiZe on 13-9-5.
// Copyright (c) 2013年
BlackCode. All rights reserved.
//
#import
"GroupTableViewController.h"
@interface
GroupTableViewController ()
@end
@implementation
GroupTableViewController
-
(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.
// 設(shè)置標(biāo)題
self.title
= @"UITableView Group
的使用";
// 初始化groupTableView
self.groupTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStyleGrouped] autorelease];
//
設(shè)置表示圖的數(shù)據(jù)源委托
_groupTableView.dataSource = self;
// 設(shè)置表示圖的委托
_groupTableView.delegate = self;
// 設(shè)置groupTableView的背景顏色
UIView *groupTableViewBGView =
[[UIView alloc] initWithFrame:self.view.frame];
groupTableViewBGView.backgroundColor = [UIColor brownColor];
_groupTableView.backgroundView = groupTableViewBGView;
[groupTableViewBGView
release], groupTableViewBGView
= nil;
// 添加到當(dāng)前視圖中
[self.view
addSubview:_groupTableView];
//
在導(dǎo)航欄上添加一個(gè)編輯按鈕放在右上角
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"編輯"
style:UIBarButtonItemStyleBordered
target:self action:@selector(toggleButton:)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release], editButton = nil;
//
在導(dǎo)航欄上添加一個(gè)提示按鈕放在左上角
UIBarButtonItem *hintLeftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self action:nil];
self.navigationItem.leftBarButtonItem = hintLeftButton;
[hintLeftButton release], hintLeftButton = nil;
}
-
(void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
#pragma mark - 實(shí)現(xiàn)---編輯按鈕的toggleButton:方法
-
(void)toggleButton:(UIBarButtonItem
*) sender {
if
(_groupTableView.isEditing == YES)
{
[_groupTableView setEditing:NO
animated:YES];
sender.title
= @"編輯";
} else {
[_groupTableView setEditing:YES
animated:YES];
sender.title
= @"完成";
}
}
#pragma mark - 重寫----設(shè)置有groupTableView有幾個(gè)分區(qū)
-
(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
return 4; //
返回值是多少既有幾個(gè)分區(qū)
}
#pragma mark - 重寫----設(shè)置每個(gè)分區(qū)有幾個(gè)單元格
-
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//
分別設(shè)置每個(gè)分組上面顯示的單元格個(gè)數(shù)
switch (section) {
case
0:
return 3;
break;
case
1:
return 2;
break;
case
2:
return 5;
break;
case
3:
return 10;
break;
default:
break;
}
return 10;
}
#pragma mark - 重寫----設(shè)置每個(gè)分組單元格中顯示的內(nèi)容
-
(UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 設(shè)置一個(gè)標(biāo)示符
static NSString *cell_id = @"cell_id";
//
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cell_id];
// 判斷cell是否存在
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cell_id]
autorelease];
cell.accessoryType =
UITableViewCellAccessoryDetailDisclosureButton;
}
//
分別給每個(gè)分區(qū)的單元格設(shè)置顯示的內(nèi)容
cell.textLabel.text = [NSString stringWithFormat:@"section = %d, row = %d",
indexPath.section,
indexPath.row];
return cell;
}
#pragma mark - 重寫----設(shè)置哪個(gè)單元格被選中的方法
-
(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"單元格"
message:[NSString stringWithFormat:@"section =
%d, row = %d",
indexPath.section,
indexPath.row] delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確認(rèn)",
nil];
[alertView show];
[alertView release], alertView = nil;
}
#pragma mark - 重寫----設(shè)置每個(gè)單元格上面的按鈕的點(diǎn)擊方法
-
(void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
// 實(shí)例化DetailViewController
DetailViewController *detailVC = [[DetailViewController
alloc] init];
// 添加出一個(gè)導(dǎo)航欄
UINavigationController *navigtion = [[UINavigationController
alloc] initWithRootViewController:detailVC];
// 設(shè)置導(dǎo)航欄顏色
navigtion.navigationBar.tintColor =
[UIColor purpleColor];
//
設(shè)置模態(tài)視圖彈出的動(dòng)畫效果
navigtion.modalTransitionStyle =
UIModalTransitionStyleCrossDissolve;
// 直接使用self呈現(xiàn)一個(gè)試圖
[self
presentViewController:navigtion
animated:YES completion:nil];
// 釋放內(nèi)存
[detailVC release], detailVC = nil;
}
#pragma mark - 重寫----設(shè)置標(biāo)題和標(biāo)注的高度
-
(CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
-
(CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section {
return 20.0f;
}
#pragma mark - 重寫----設(shè)置標(biāo)題和標(biāo)注
#pragma mark - 重寫----設(shè)置自定義的標(biāo)題和標(biāo)注
-
(UIView
*)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];;
headerLabel.font = [UIFont boldSystemFontOfSize:18.0f];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = @"
我是標(biāo)題";
headerLabel.textColor = [UIColor blackColor];
return headerLabel;
}
-
(UIView
*)tableView:(UITableView
*)tableView viewForFooterInSection:(NSInteger)section {
UILabel *footerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];;
footerLabel.font = [UIFont boldSystemFontOfSize:15.0f];
footerLabel.backgroundColor = [UIColor clearColor];
footerLabel.textAlignment = NSTextAlignmentCenter;
footerLabel.text = @"(*^__^*)
嘻嘻……";
footerLabel.textColor = [UIColor whiteColor];
return footerLabel;
}
#pragma mark - 重寫----設(shè)置單元格可以排序
-
(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
#pragma mark - 重寫----設(shè)置排序執(zhí)行的方法
-
(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
}
#pragma mark - 重寫----設(shè)置單元格隔行換顏色
-
(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath
*)indexPath {
if (indexPath.row % 2
== 0) {
cell.backgroundColor
= [UIColor orangeColor];
} else {
cell.backgroundColor
= [UIColor yellowColor];
}
}
#pragma mark - 重寫----設(shè)置groupTableView可以編輯
-
(BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
#pragma mark
- 重寫----設(shè)置編輯狀態(tài)下左面顯示的是加號(hào)、減號(hào)、還是空白
-
(UITableViewCellEditingStyle)tableView:(UITableView
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return
UITableViewCellEditingStyleDelete;
}
#pragma mark - 重寫----根據(jù)編輯的狀態(tài)來(lái)執(zhí)行什么操作
-
(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath
*)indexPath {
//
根據(jù)編輯狀態(tài)進(jìn)行相應(yīng)的操作
if
(editingStyle ==
UITableViewCellEditingStyleInsert)
{ //
進(jìn)行插入的操作
} else if (editingStyle ==
UITableViewCellEditingStyleDelete)
{ //
進(jìn)行刪除的操作
}
}
#pragma mark - 重寫----dealloc方法
-
(void)dealloc {
[_groupTableView release], _groupTableView = nil;
[super dealloc];
}
@end
|