UITableview中怎么找到每个cell
2015-11-23 15:42
369 查看
一个朋友问我:我在每个cell中都添加了两个按钮(记为btnA和btnB),点击btnA时,对应的cell中添加一个子控件,再点击btnB时,对应的cell中的子控件就移除,怎么做到?
百度了一下,发现了解决办法:
首先,创建btn时,给每个btn加一个tag值
给添加了红色的几行代码,就搞定了!
我去,这个方法亮了,我已经被闪瞎了!
百度了一下,发现了解决办法:
首先,创建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; }
//添加子控件按钮代码 -(void)btnBClick:(UIButton *)btn { NSString * path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp4"]; NSURL * url=[NSURL fileURLWithPath:path]; _mp=[[MPMoviePlayerViewController alloc]initWithContentURL:url]; _mp.view.backgroundColor=[UIColor purpleColor]; _mp.moviePlayer.controlStyle=MPMovieControlStyleDefault; _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]; [_tableView reloadData]; }
//移除子控件按钮代码 -(void)btnAClick:(UIButton *)btn { 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 } else return; }
给添加了红色的几行代码,就搞定了!
我去,这个方法亮了,我已经被闪瞎了!
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; cell.detailTextLabel.text= [self.dateFormatter stringFromDate:self.pickerView.date];
相关文章推荐
- NSNotificationCenter(通知)与Key-Value Coding (KVC)与Key-Value Observing (KVO)
- UIImageView中图片尺寸模式
- Kibana User Guide [4.2] » Settings » Managing Fields
- [iOS]UITableView优化技巧
- 深度学习String、StringBuffer、StringBuilder
- impdp卡住,遇到等待事件Streams AQ: enqueue blocked on low memory
- [Leetcode]Range Sum Query - Immutable
- iOS8新特性 UIPresentationController(一)
- Kibana User Guide [4.2] »Settings
- Lesson 11 Not guilty
- UE4简单巡逻BehaviorTree的构建
- String,StringBuffer,StringBuilder
- easyui textbox 添加 onblur 失去焦点事件
- 定制UIButton中文字和图片的位置
- 053-14 You have a very large table that your users access frequently. Which of the following advisor
- iOS UISearchBar简单使用与简介
- 移动设备上的UI布局新方向(思路)
- [iOS]UIScrollView 使用 Autolayout
- CheckListBox获取多选的项文本和Value值
- UIButton文字的显示位置,字体的大小