zoukankan      html  css  js  c++  java
  • 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

    在自定义UITableViewCell中创建了一个按钮。

    想在点击该按钮时知道该按钮所在的cell在TableView中的行数。就是cell的 indexPath.row

    两种方法都很好。
    -(IBAction):(id)sender
    {
        NSLog(@"MyRow:%d",[self.table indexPathForCell:((TableViewCell*)[[sender   superview]superview])].row); //这个方便一点点,不用设置tag。
        NSLog(@"MyRow:%d",((TableViewCell*)[[sender superview]superview]).tag);
        //这个需要加载cell时设置tag.不过也很方便。
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView
            cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //删除cell.contentView中所有内容,避免以下建立新的重复
        int i = [[cell.contentView subviews] count] - 1;
        for(;i >= 0 ; i--)
        {
             [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
        }


        //添加button
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        [but setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
        [but setFrame:CGRectMake(280, 10, 30, 30)];
        [but setAlpha:0.8];
        [but addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:but];


        //设置Tag为cell 对应的indexPath row
        for(id view in subviews)
        {
             if([view isKindOfClass:[UIButton class]])
             {
                 [view setTag:[indexPath row]];
                 //[cell.contentView bringSubviewToFront:view];
             }
        }
    }



    //Button事件
    -(void)del:(id)sender
    {
        for(UITableViewCell *cell in visiblecells)
        {
            if(cell.tag == button.tag)
        {
        //button.tag就是对应的[indexPath.row
     
    转自:http://blog.sina.com.cn/s/blog_708663ad010153ul.html
  • 相关阅读:
    (转)Linux系统调用和库函数调用的区别
    一个“梦想实践重度障碍者”的思考
    按字节输出数据
    内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理]
    VimdiffVIM的比较和合并工具
    [每天进步一点 流水账]回顾总结
    计算机就业方向
    ofstream和ifstream详细用法(转)
    ECMAScript 运算符关系运算符
    ECMAScript 语句标签语句
  • 原文地址:https://www.cnblogs.com/CityPe/p/6189209.html
Copyright © 2011-2022 走看看