iOS tableView事件方法

//

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


源代码下载 http://download.csdn.net/detail/aa741649143/5908187
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值