UIButton 点击时无法附带自身参数的解决办法
2014-04-01 14:28
225 查看
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:UITableViewCellStyleSubtitlereuseIdentifier: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:UITableViewCellStyleSubtitlereuseIdentifier: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);
}
这样就可以绕过之前的阻碍继续获取当前点击行数了.
http://blog.csdn.net/loseleo/article/details/8984624
目前有两个方法来处理:
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:UITableViewCellStyleSubtitlereuseIdentifier: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:UITableViewCellStyleSubtitlereuseIdentifier: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);
}
这样就可以绕过之前的阻碍继续获取当前点击行数了.
http://blog.csdn.net/loseleo/article/details/8984624
相关文章推荐
- UIButton 点击时无法附带自身参数的解决办法
- iOS UIButton点击事件传递参数的解决办法
- iOS UIButton点击事件传递多个参数的解决办法 -----使用关联函数
- iOS开发-工具:Xcode7升级之后插件无法使用与不小心点击Skipbundle的解决办法
- 【内容转载】iOS开发----Xcode7升级之后插件无法使用与不小心点击Skipbundle的解决办法
- DUI 响应选择行事件后,在本行再次点击无法响应选择行事件的解决办法
- 吐哈录井远传无法传输发送某参数的解决办法
- iOS UIButton超出父视图无法点击解决方法
- myeclipse tomcat无法部署finish无法点击为灰色解决办法
- Struts2 自定义拦截器栈后无法得到表单参数之解决办法
- JLink 修复过程中出现write flash一直无法点击 解决办法
- 关于因连续快速点击UIButton,造成内存溢出的一种解决办法
- 关于因连续快速点击UIButton,造成内存溢出的一种解决办法
- Spring MVC 使用redirect跳转页面,页面无法使用jstl接收参数的 解决办法
- Struts2 自定义拦截器栈后无法得到表单参数之解决办法
- iOS开发----Xcode7升级之后插件无法使用与不小心点击Skipbundle的解决办法【转载】
- vs2015中c++中multiset容器定义对象时参数列表中显式调用greater无法被识别的解决办法
- URL带多个参数&无法解析的解决办法!+
- 进行animation时点击事件无法响应的解决办法(animation进行时默认是禁止交互)
- 页面加UIPanGestureRecognizer手势后,无法响应tableViewCell点击事件的解决办法