//
// ViewController.m
// TableViewDemo
//
// Created by linpeng on 13-8-11.
// Copyright (c) 2013年 linpeng. All rights reserved.
//
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
//添加代理和数据源 如果使用代码生成的tableview 就要下面 这两句 如果是拖来的控件 在叶面上直接关联了 数据源 与代理 就不需要
// self.tableView.dataSource = self;
// self.tableView.delegate= self;
//修改第一行距离导航栏de高度 60
self.tableView.tableHeaderView = [[[UIViewalloc]initWithFrame:CGRectMake(0,0,5,60)]autorelease];
//添加导航按钮 编辑按钮
self.navigationItem.rightBarButtonItem =self.editButtonItem;
NSString *path = [[NSBundlemainBundle]pathForResource:@"BookList"ofType:@"plist"];
NSArray *bookInfo = [NSArrayarrayWithContentsOfFile:path];
// NSLog(@"%@",bookInfo);
self.arr=[[[NSMutableArrayalloc]init]autorelease];
for (int i=0; i<bookInfo.count; i++) {
//ios6特性
NSDictionary *dic = bookInfo[i];
Book *book = [[Bookalloc]init];
book.name = [dicobjectForKey:@"name"];
book.auth = [dicobjectForKey:@"auth"];
book.image = [dicobjectForKey:@"image"];
book.descript = [dicobjectForKey:@"descript"];
self.arr[i] = book;
[bookrelease];
}
}
//提交编辑时候要做的事情 点击删除de时候做的事情
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据具体的样式或者添加的事件来最对应的处理
if (editingStyle ==UITableViewCellEditingStyleDelete) {
NSLog(@"删除样式的时候做些什么");
}
if (editingStyle ==UITableViewCellEditingStyleInsert) {
NSLog(@"添加样式的时候做些什么");
}
}
//编辑杨式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete; //删除的字样
// return UITableViewCellEditingStyleInsert; //加号的字样
}
//是否可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
returnNO;
}
else
{
//如果是删除的话 只要滑动就会出现 delete 是add样式de话则不行
return YES;
}
}
//自带的设置编辑方法 重写
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
NSLog(@"%d",editing);
[supersetEditing:editinganimated:YES];
[self.tableViewsetEditing:editinganimated:YES];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//一个分组有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.arr.count;
}
//选中cell时候de事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailViewController *vc = [[[detailViewControlleralloc]init]autorelease];
Book *book = [self.arrobjectAtIndex:indexPath.row];
vc.name =book.name;
vc.pic = book.image;
vc.oauth = book.auth;
vc.detal =book.descript;
[self.navigationControllerpushViewController:vcanimated:YES];
}
//table几个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return1;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
//cell内容绑定
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *identifer =@"shitCell";
//重用 队列 可重用的 标shi
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifer];
if(cell==nil)
{ //UITableViewCellStyleSubtitle自带字标题的
//系统自定义decell
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifer];
//使用自定义的cell
// cell = [[[NSBundle mainBundle] loadNibNamed:@"myCell" owner:nil options:nil] lastObject];
}
//------------------------------------------------------------celld的复用
// cell.textLabel.text = [NSString stringWithFormat:@"%@-%d",cell.textLabel.text,indexPath.row];
Book *book = [self.arrobjectAtIndex:indexPath.row];
cell.textLabel.text = book.name;
UIImage *image = [UIImageimageNamed:book.image];
cell.detailTextLabel.text = book.auth;
cell.imageView.image =image;
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle =UITableViewCellSelectionStyleNone;
//重写时候的Cell填充数据使用下面这样的方法
// [(UILabel*)[cell viewWithTag:1] setText:@"asdsa"];
// [(UILabel*)[cell viewWithTag:2] setText:@"dasdas"];//
//
// NSString *headpic = [[NSStringalloc]initWithFormat:@"卡通头像图标下载%d",i ];
// [(UIImageView*)[cellviewWithTag:0]setImage:[UIImageimageNamed:headpic]];//
return cell;
}
//右边小尖头de事件
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
detailViewController *vc = [[[detailViewControlleralloc]init]autorelease];
Book *book = [self.arrobjectAtIndex:indexPath.row];
vc.name =book.name;
vc.pic = book.image;
vc.oauth = book.auth;
vc.detal =book.descript;
[self.navigationControllerpushViewController:vcanimated:YES];
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return130;
}
/*此时删除按钮为Delete,如果想显示为“删除”中文的话,则需要实现
UITableViewDelegate 中的- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath方法*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return@"删除";
}
- (void)dealloc {
[_tableViewrelease];
[_arrrelease];
[superdealloc];
}
@end