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

简介: <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 前面写的tableview 什么都干不了</p> <p style="margin-top:0px; margin-

前面写的tableview 什么都干不了

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


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


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

{

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

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:titileStringdelegate: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:titileStringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

    [alert show];


}



效果如下图:





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


我们会用到这个函数


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

{


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

    if (indexPath.row ==0) {

        returnnil;

    }

    return indexPath;

}

目录
相关文章
iOS15 tableView顶部空白解决办法
iOS15 tableView顶部空白解决办法
96 0
|
开发工具 iOS开发 git
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
206 0
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
|
iOS开发
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
269 0
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
|
Android开发 iOS开发
关于监听微信浏览器返回按钮事件处理安卓IOS通用
关于监听微信浏览器返回按钮事件处理安卓IOS通用
328 0
关于监听微信浏览器返回按钮事件处理安卓IOS通用
|
iOS开发
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
160 0
|
iOS开发
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
135 0
|
iOS开发
iOS开发 - 让tableView不能下拉刷新,可以上拉加载
iOS开发 - 让tableView不能下拉刷新,可以上拉加载
298 0
|
iOS开发
iOS之UIPickerView滚动事件
在开发中,我们会用到pickerview滚动条,那如何监听到pickview的滚动事件呢,之前开发就遇到过一个问题,快速滑动秒选确定按钮,地址显示不对的问题,解决办法为在点选确定辅助按钮的时候判断当时的pickerView是否正在滚动,如果在滚动则不允许触发点选确定后的其他操作。
365 0
iOS之UIPickerView滚动事件
|
iOS开发
iOS tableView实现顶部图片拉伸效果
这篇文章主要为大家详细介绍了iOS tableView实现顶部图片拉伸效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
|
iOS开发
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
972 0