星空网 > 软件开发 > 操作系统

UITableview中怎么找到每个cell

一个朋友问我:我在每个cell中都添加了两个按钮(记为btnA和btnB),点击btnA时,对应的cell中添加一个子控件,再点击btnB时,对应的cell中的子控件就移除,怎么做到?

 

百度了一下,发现了解决办法:

首先,创建btn时,给每个btn加一个tag值

//创建cell方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  static NSString * iden=@"iden";  _cell=[tableView dequeueReusableCellWithIdentifier:iden];  if (_cell==nil) {    _cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];  }    UIButton * btnA=[UIButton buttonWithType:UIButtonTypeCustom];  btnA.frame=CGRectMake(0, 0, 50, 20);  btnA.tag = 1000 + indexPath.row;  btnA.backgroundColor=[UIColor greenColor];  [btnA addTarget:self action:@selector(btnBClick:) forControlEvents:UIControlEventTouchUpInside];    [_cell.contentView addSubview:btnA];    UIButton * btnB=[UIButton buttonWithType:UIButtonTypeCustom];  btnB.tag = 2000 + indexPath.row;  btnB.frame=CGRectMake(100, 0, 50, 20);  btnB.backgroundColor=[UIColor redColor];  [btnB addTarget:self action:@selector(btnAClick:) forControlEvents:UIControlEventTouchUpInside];  [_cell.contentView addSubview:btnB];  return _cell;}

 1 //添加子控件按钮代码 2 -(void)btnBClick:(UIButton *)btn 3 { 4   NSString * path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp4"]; 5   NSURL * url=[NSURL fileURLWithPath:path]; 6   _mp=[[MPMoviePlayerViewController alloc]initWithContentURL:url]; 7   _mp.view.backgroundColor=[UIColor purpleColor]; 8   _mp.moviePlayer.controlStyle=MPMovieControlStyleDefault; 9   10   _mp.view.frame=CGRectMake(0, 0, self.view.frame.size.width, 200);11   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag - 1000 inSection:0];12   UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];// 竟然还有这个方法,第一次用13   [cell.contentView addSubview:_mp.view];14   [_tableView reloadData];15 }

 1 //移除子控件按钮代码 2 -(void)btnAClick:(UIButton *)btn 3 { 4   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag - 2000 inSection:0]; 5   UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath]; 6   if ([cell.contentView.subviews containsObject:_mp.view]) { 7     [_mp.view removeFromSuperview]; 8   } 9   else10     return;11 }

给添加了红色的几行代码,就搞定了!




原标题:UITableview中怎么找到每个cell

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

特朗普称15日前TikTok没卖就关门,抖音引流还有未来吗?:https://www.goluckyvip.com/news/3553.html
Shopee 9.9大促战报,3点操作单日销量提高10倍:https://www.goluckyvip.com/news/3554.html
浅谈东南亚电商的新基建:电子支付:https://www.goluckyvip.com/news/3555.html
特朗普:关闭or卖掉!TikTok的回应是......:https://www.goluckyvip.com/news/3556.html
疫情期间多平台倒闭,Lazada季度订单却同比增长超100%:https://www.goluckyvip.com/news/3557.html
泰嘉物流:https://www.goluckyvip.com/news/3558.html
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流