关于cell 上添加 button 等控件的一些问题

//获取点击的button的父视图

    MMAppCell* cell=(MMAppCell*)[[sender superview] superview];

//获取table

    UITableView* table=(UITableView*)[cell superview];

//获取table的indexpath

    NSIndexPath* index=[table indexPathForCell:cell];

    

//数据源

    NSDictionary *dic=[[idsDiction objectForKey:[NSString stringWithFormat:@"%@",[[idsDiction allKeys] objectAtIndex:index.section]]] objectAtIndex:index.row];

   

//获取url

    NSURL *url = [NSURL URLWithString:[dic objectForKey:@"tuijianurl"]];

    [[UIApplication sharedApplication] openURL:url];

=======================================

iOS tableview的每个cell中放了一个button,如何才能知道那个button被点击了?

2014-03-07 13:06yy290595955 | 浏览 7433 次
tableview中有N个section,每个section有N个cell,每个cell中有一个button,假如这些button的点击事件都是btnClick,如何能够知道哪个button被点击了?最好能通过indexPath这个参数得到。我想到的是重写UIButton,给UIButton一个属性是indexPath,但是实现起来遇到了困难,在网上也没有找到具体的实例,请问有没有更好的办法,或者告诉我具体怎么重写UIButton。
 
2014-03-08 19:57 提问者采纳
 
1 你的想法可以解决问题  自己新建一个类 继承UIButton  里面加个属性 int就行 然后设置cell的时候 让这个int等于 indexPath.row (比较麻烦 需要自定义button 也有可能要自定义cell)
2 给每个button设置一个tag值 就是 indexPath.row+固定常数(实现简单 代码不健壮逻辑复杂了不好处理)
3自定义cell 然后btnClick事件 在cell里处理 生成cell的时候 注入依赖和上下文 如果cell 种类有多个 用工厂模式抽象 需要用协议写接口 还要注意 引用context的时候要区分 weak 和strong (避免循环引用) 如果协议不熟悉 也可以用通知中心回调ViewController (这种方法 对开发者基础要求教高 但是比较推荐 代码健壮 重用性高 封装较好 )
4 用block封装 处理事件 类似方法1 需要重写btn(不推荐 内存占用大 )
还有其他方法 不一一列举了 到底怎么搞 要多动脑 根据实际情况来 IOS的开发是比较自由的 实现一个功能 有很多条路

=============================

-(void)camera:(UIButton *)sender

{

    NSLog(@"照相");

    NSInteger cellRow = [self.tableView indexPathForCell:((BrandReportCell*)[[sender superview]superview])].row;

//    NSLog(@"MyRow:%ld",cellRow); //这个方便一点点,不用设置tag。

    

    //获取点击的button的父视图

    cell=(BrandReportCell*)[[sender superview] superview];

    [cell.cameraBtn setImage:[UIImage imageNamed:@"27-1_0.png"] forState:UIControlStateNormal];

    

//    [cell.cameraBtn setImage:[UIImage imageNamed:@"27-1_0.png"] forState:UIControlStateNormal];

    

    

//    BrandReportInformation *reportInfo = self.listArray[cellRow];

//    SingleData *single = [SingleData sharedSingleData];

//    [self netWorkquickShotWithAccessToken:single.accessToken param:reportInfo.snapshotParamString];

    

}

原文地址:https://www.cnblogs.com/dexjay/p/4835792.html