ios tableView那些事 (七) 给tableView添加响应事件

前面写的tableview 什么都干不了

现在给它添加响应事件吧!这才是它的真正用处


先给他简单的加个响应事件吧!


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *titileString = [arrayobjectAtIndex:[indexPath row]];  //这个表示选中的那个cell上的数据

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:titileString delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

       [alert show];

}

效果如下图


其实在触发在cell访问的时候里面有个默认的style的

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{


 

      cell.accessoryType =    UITableViewCellAccessoryNone,                   // don't show any accessory view   //这个就是那个默认的吧

//    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track

//    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks

//    UITableViewCellAccessoryCheckmark


}

我们接下来看看其他的3种效果

    cell.accessoryType =    UITableViewCellAccessoryDisclosureIndicator;

 大多的时候表示点击cell 可以pus 到下个viewcontrol 



 cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton

这个是在cell 的右侧添加个button 来展示或触发其他的展示消失或推送! 但是这个是系统的button 大多数的应用都是自己定义的button我下说下系统的吧

这个时候我们需要另个一个delegate 和触发cell 的函数是不同的


-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    NSString *titileString = [NSString stringWithFormat:@"你点击了按钮%@",[array objectAtIndex:[indexPath row]]];

    UIAlertView *alert = [[ UIAlertView alloc]initWithTitle:@"提示" message:titileString delegate:self    cancelButtonTitle:@"OK"otherButtonTitles: nil];

    [alert show];

 }


看下点击的效果吧



 cell.accessoryType = UITableViewCellAccessoryCheckmark;


这个是选择数据时候 告诉我们选择了那行,感觉有点想我们加图片那节给cell加背景图的感觉!

先声明个全局变量

@property (strong,nonatomic)NSIndexPath *lastpath ;



让后在下面函数里面添加相应代码

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPathrow];

    NSUInteger oldRow = [lastpathrow];

 //如何点击当前的cell 最右边就会出现一个对号 ,在点击其他的cell 对号显示当前,上一个小时

  cell.accessoryType =  (row==oldRow &&lastpath != nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

   int newRow = [indexPath row];

   int oldRow = (lastpath != nil) ? [lastpath row] : -1;  

   if (newRow != oldRow) {

       UITableViewCell *newCell = [tableView cellForRowAtIndexPath:

                                   indexPath];

       newCell.accessoryType = UITableViewCellAccessoryCheckmark;

       

       UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:

                                    lastpath];

      oldCell.accessoryType = UITableViewCellAccessoryNone;

        

       lastpath = indexPath;

    }

  // 取消选择状态

   [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


效果如下图



(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{


   mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

   mybutton.frame = CGRectMake(0, 0, 100, 25);

   [mybutton setTitle:@"myButton" forState:UIControlStateNormal];

  [mybutton setBackgroundImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];

  [mybutton addTarget:self action:@selector(myBtnClick:event:) forControlEvents:UIControlEventTouchUpInside];

    cell.accessoryView = mybutton;


}

-(void)myBtnClick:(id)sender event:(id)event

{

    NSSet *touches = [eventallTouches];   // 把触摸的事件放到集合里

    

    UITouch *touch = [touchesanyObject];   //把事件放到触摸的对象了

    

   CGPoint currentTouchPosition = [touchlocationInView:self.tableview]; //把触发的这个点转成二位坐标

    

    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:currentTouchPosition]; //匹配坐标点

    if(indexPath !=nil)

    {

        [selftableView:self.tableviewaccessoryButtonTappedForRowWithIndexPath:indexPath];

    }

    

    

}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    NSString *titileString = [NSStringstringWithFormat:@"你点击了按钮%@",[arrayobjectAtIndex:[indexPath row]]];

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:titileString delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

    [alert show];


}



效果如下图:





如果我不想指定的cell 有触发事件呢


我们会用到这个函数


-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

{


//我让第一个cell 点击的时候没有反应

    if (indexPath.row ==0) {

        returnnil;

    }

    return indexPath;

}


  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值