zoukankan      html  css  js  c++  java
  • UITableViewCell中添加多个UIButton实例

         在项目中又遇到坑了.UITableViewCell中添加了2个UIButton.点击事件添加在Cell中.cell设置代理,TableViewController类接受代理,并执行相应的方法.根据superView 找到cell,根据cell找到相应的indexpath. 

      贴代码:

      

    - (void)editItem:(UIButton *)sender
    {
        UITableViewCell * cell = (UITableViewCell *)[[[sender superview] superview] superview];
        NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"index row %d", [indexPath row]);
        ......
    }
    

       

      以上方法只有iOS7能用.iOS8得去掉一个superview,如下图:

    - (void)editItem:(UIButton *)sender
    {
        UITableViewCell * cell = (UITableViewCell *)[[sender superview] superview];
        NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"index row %d", [indexPath row]);
        ......
    }
    

      坑啊....我怎么知道iOS8哪个版本去掉的视图?

      不过还好.问了朋友,推荐用响应者链.

    - (UITableViewCell *)tableViewCellForbutton:(UIButton *)button
    {
        for (UIView *next = [button superview] ; next; next = next.superview) {
            UIResponder *nextResponder = [next nextResponder];
            if ([nextResponder isKindOfClass:[UITableViewCell class]]) {
                return (UITableViewCell*)nextResponder;
            }
        }
        return nil;
    }
    //在代理方法中写入如下内容,即可完美找到cell.
    - (void)editItem:(UIButton *)sender
    {
        UITableViewCell * cell = [self tableViewCellForbutton:sender];
        NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"index row %d", [indexPath row]);
      .......
    }
    

    以上方法都是纯代码的方法,StoryBoard(SB)和XIB的方式,还没试过.目前朋友试过SB的方法可以用2个superview找到cell,不被iOS的版本所限制.

  • 相关阅读:
    ZH奶酪:PHP 使用DOMDocument抓取网页
    PHP Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,
    ZH奶酪:PHP 执行时间Fatal error: Maximum execution time of...
    ZH奶酪:PHP (爬虫)下载图片
    ZH奶酪:PHP的cURL库
    PHP 字符串编码的转换
    PHP http_build_query()方法
    ZH奶酪:使用PHP调用REST API
    PHP全局变量
    HTML页面跳转的5种方式
  • 原文地址:https://www.cnblogs.com/xclidongbo/p/4271478.html
Copyright © 2011-2022 走看看