//
// 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.
// 设置标题
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];
// 设置表示图的数据源委托
_groupTableView.dataSource = self;
// 设置表示图的委托
_groupTableView.delegate = self;
// 设置groupTableView的背景颜色
UIView *groupTableViewBGView = [[UIView alloc]initWithFrame:self.view.frame];
groupTableViewBGView.backgroundColor = [UIColor brownColor];
_groupTableView.backgroundView = groupTableViewBGView;
[groupTableViewBGView release], groupTableViewBGView = nil;
// 添加到当前视图中
[self.view addSubview:_groupTableView];
// 在导航栏上添加一个编辑按钮放在右上角
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑"style:UIBarButtonItemStyleBordered target:self action:@selector(toggleButton:)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release], editButton = nil;
// 在导航栏上添加一个提示按钮放在左上角
UIBarButtonItem *hintLeftButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:selfaction:nil];
self.navigationItem.leftBarButtonItem = hintLeftButton;
[hintLeftButton release], hintLeftButton = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 实现---编辑按钮的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 - 重写----设置有groupTableView有几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4; // 返回值是多少既有几个分区
}
#pragma mark - 重写----设置每个分区有几个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 分别设置每个分组上面显示的单元格个数
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 - 重写----设置每个分组单元格中显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 设置一个标示符
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;
}
// 分别给每个分区的单元格设置显示的内容
cell.textLabel.text = [NSString stringWithFormat:@"section = %d, row = %d", indexPath.section, indexPath.row];
return cell;
}
#pragma mark - 重写----设置哪个单元格被选中的方法
- (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:@"确认",nil];
[alertView show];
[alertView release], alertView = nil;
}
#pragma mark - 重写----设置每个单元格上面的按钮的点击方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
// 实例化DetailViewController
DetailViewController *detailVC = [[DetailViewController alloc] init];
// 添加出一个导航栏
UINavigationController *navigtion = [[UINavigationController alloc]initWithRootViewController:detailVC];
// 设置导航栏颜色
navigtion.navigationBar.tintColor = [UIColor purpleColor];
// 设置模态视图弹出的动画效果
navigtion.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// 直接使用self呈现一个试图
[self presentViewController:navigtion animated:YES completion:nil];
// 释放内存
[detailVC release], detailVC = nil;
}
#pragma mark - 重写----设置标题和标注的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 20.0f;
}
#pragma mark - 重写----设置标题和标注
#pragma mark - 重写----设置自定义的标题和标注
- (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 = @" 我是标题";
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 - 重写----设置单元格可以排序
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
#pragma mark - 重写----设置排序执行的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
}
#pragma mark - 重写----设置单元格隔行换颜色
- (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 - 重写----设置groupTableView可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
#pragma mark - 重写----设置编辑状态下左面显示的是加号、减号、还是空白
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
#pragma mark - 重写----根据编辑的状态来执行什么操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
// 根据编辑状态进行相应的操作
if (editingStyle == UITableViewCellEditingStyleInsert) { // 进行插入的操作
} else if (editingStyle == UITableViewCellEditingStyleDelete) { // 进行删除的操作
}
}
#pragma mark - 重写----dealloc方法
- (void)dealloc {
[_groupTableView release], _groupTableView = nil;
[super dealloc];
}
@end