UIButton 点击时无法附带自身参数的解决办法
2013-05-28 14:45
351 查看
UIButton的 taget函数只能是 不带参数或者带一个默认UIButton类型的参数.当把UIButton加入table的时候获取点击时候的行数就是个问题.
目前有两个方法来处理:
1:用UIButton自身的tag
也就是说在创建UIButton的时候设置tag为tableview的index.row.这样在回调函数里面根据tag就可以处理.但是这个方法有个弊端就是如果tag被用来做tableview的动态加载标示,那就用第二种方法去解决了
示例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString
stringWithFormat:@"Cell%d", indexPath.row];//这里按照每个row来标示cell,所以每个cell都是独立的,没有复用,数量少的时候可以这么处理
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *btnBuy = (UIButton*)[cell
viewWithTag:indexPath.row];
if(!btnBuy)
{
XXXXX
btnBuy.tag =indexPath.row;
[btnBuy
addTarget:self
action:@selector(onClickBuy:)
forControlEvents:UIControlEventTouchUpInside];
}
[btnBuy setImage:XXXX];
return cell;
}
-(void)onClickBuy:(UIButton*)btn
{
NSLog(@"点击了第%d",btn.tag);
}
以上这种就可以获取到当前点击的行数.
2:复用cell的情况下的解决办法:
解决办法其实就给uibutton增加一个子类view,设置子类view的tag
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =
@"Cell";//注意这里这样写就是复用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *btnBuy = (UIButton*)[cell
viewWithTag:4000];
if(!btnBuy)
{
XXXXX
btnBuy.tag =4000;
[btnBuy addTarget:self
action:@selector(onClickBuy:)
forControlEvents:UIControlEventTouchUpInside];
UIView *tagView = [[UIView
alloc]init];
tagView.tag = indexPath.row;
[btnBuy
addSubview:tagView];
[tagView
release];
}
[btnBuy setImage:XXXX];
return cell;
}
-(void)onClickBuy:(UIButton*)btn
{
UIView *tagView = [[btn
subviews] objectAtIndex:0];
NSLog(@"点击了第%d",tagView.tag);
}
这样就可以绕过之前的阻碍继续获取当前点击行数了.
目前有两个方法来处理:
1:用UIButton自身的tag
也就是说在创建UIButton的时候设置tag为tableview的index.row.这样在回调函数里面根据tag就可以处理.但是这个方法有个弊端就是如果tag被用来做tableview的动态加载标示,那就用第二种方法去解决了
示例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString
stringWithFormat:@"Cell%d", indexPath.row];//这里按照每个row来标示cell,所以每个cell都是独立的,没有复用,数量少的时候可以这么处理
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *btnBuy = (UIButton*)[cell
viewWithTag:indexPath.row];
if(!btnBuy)
{
XXXXX
btnBuy.tag =indexPath.row;
[btnBuy
addTarget:self
action:@selector(onClickBuy:)
forControlEvents:UIControlEventTouchUpInside];
}
[btnBuy setImage:XXXX];
return cell;
}
-(void)onClickBuy:(UIButton*)btn
{
NSLog(@"点击了第%d",btn.tag);
}
以上这种就可以获取到当前点击的行数.
2:复用cell的情况下的解决办法:
解决办法其实就给uibutton增加一个子类view,设置子类view的tag
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =
@"Cell";//注意这里这样写就是复用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *btnBuy = (UIButton*)[cell
viewWithTag:4000];
if(!btnBuy)
{
XXXXX
btnBuy.tag =4000;
[btnBuy addTarget:self
action:@selector(onClickBuy:)
forControlEvents:UIControlEventTouchUpInside];
UIView *tagView = [[UIView
alloc]init];
tagView.tag = indexPath.row;
[btnBuy
addSubview:tagView];
[tagView
release];
}
[btnBuy setImage:XXXX];
return cell;
}
-(void)onClickBuy:(UIButton*)btn
{
UIView *tagView = [[btn
subviews] objectAtIndex:0];
NSLog(@"点击了第%d",tagView.tag);
}
这样就可以绕过之前的阻碍继续获取当前点击行数了.
相关文章推荐
- UIButton 点击时无法附带自身参数的解决办法
- iOS UIButton点击事件传递参数的解决办法
- iOS UIButton点击事件传递多个参数的解决办法 -----使用关联函数
- append之后无法触发点击事件的解决办法
- iOS开发----Xcode7升级之后插件无法使用与不小心点击Skipbundle的解决办法【转载】
- vs2015中c++中multiset容器定义对象时参数列表中显式调用greater无法被识别的解决办法
- UITapGestureRecognizer和UIButton的点击事件冲突的解决办法
- 进行animation时点击事件无法响应的解决办法(animation进行时默认是禁止交互)
- 页面加UIPanGestureRecognizer手势后,无法响应tableViewCell点击事件的解决办法
- Destoon旺旺无法正常显示,点击提示“会员名不存在”的解决办法
- iOS11自定义导航栏标题,位置有偏差并且无法响应点击事件和手势的解决办法(Swift)
- android RelativeLayout 无法覆盖点击事件解决办法
- 原创IE无法打开javascript链接(点击无反应)的解决办法
- ionic开发——百度地图InfoWindow信息窗口上添加按钮,无法触发点击事件问题解决办法
- ghost中断导致光盘、U盘附带工具都无法启动解决办法
- PyCharm无法引用自身项目解决办法
- springmvc put请求无法解析请求体参数解决办法
- win8.1 开始按钮和charm栏无法点击及激活的解决办法
- 简单解决 multipart/form-data 文件上传表单中 传递参数无法获取的办法! (解决办法:动态更改表单提交URL)
- xcode的indexing功能取消,出现无法command点击跳转方法,代码变白的解决办法