如何获取ios 点击cell获取内容的UITableView的cell的内容

cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题 - 推酷
cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
============================================================
博文原创,转载请声明出处
============================================================
我们用这种方法去创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
dequeueReusableCellWithIdentifier:cellIdentifier];
UITableViewCell* cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
那么,相应我们button的selector中,我们可以通过下面的方法或侧indexpath
-(void)buttonaction:(UIButton*)sender{
UIView* v=[sender superview];//UITableViewCellContentView
UITableViewCell* cell=(UITableViewCell*)[v superview];//UITableViewCell
NSIndexPath* indexPath= [_tbSelect indexPathForCell:cell];
这里我们用了两个个superview,第一个得到UITableViewCellContentView,第二个得到相应的UITableViewCell。
同样。我们使用xib,拖进去一个UITableViewCell也是可以的
问题来了,
----------上面的方法我们成为方法A,下面的方法我们成为方法B
一次,我拖进去一个UIView,然后将class改为UITableViewCell,使用上面的方法不行。
而是只能使用一个superview
UITableViewCell* cell=(UITableViewCell*)[sender superview];//UITableViewCell
后来研究发现方法A中,使用的是系统的UITableViewCell,因此自动添加了UITableViewCellContentView
而在方法B中,我们是自己修改的,因此没有UITableViewCellContentView
打印方法:
NSLog(@&----start&);
for (UIView* v in [cell subviews]) {
NSLog(@&v:%@&,v);
打印结果:
v:&UITableViewCellContentView: 0xa517a20; frame = (0 0; 320 44); gestureRecognizers = &NSArray: 0xa518f90&; layer = &CALayer: 0xa51e450&&
14:04:12.529 demo[2572:c07] v:&UILabel: 0x10f80a60; frame = (30 11; 215 21); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 101; layer = &CALayer: 0x10f80af0&&
14:04:12.530 demo[2572:c07] v:&UICheckBox: 0x10f7f680; baseClass = UIB frame = (260 7; 30 30); opaque = NO; autoresize = RM+BM; tag = 102; layer = &CALayer: 0x10f7f740&&
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致qt 中怎样获取tableview中鼠标单击的单元格的内容_百度知道UITableView中关于cell里的按钮被点击时如何确定是哪一个cell
UITableView中关于cell里的按钮被点击时如何确定是哪一个cell
在section=10;row=1;的UITableView中,每一个cell都带有一个按钮,例如如下的图片一样
每一个cell中都有一个“进入店铺的按钮”,但是如果我点击相应的cell要进入对应的店铺如何处理呢?
如果用”- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath”这个方法的话会的确可以用“indexPath.section”定位到我点击的是哪一个section,但是会使得整个cell都能点击。如果不介意的话这个方法当然可以,下面来说一下只通过按钮来确定是哪一个section的方法。
首先,你的按钮必须要绑定你的事件,和storyBoard拖个线就行了。
然后一定要在storyBoard仔细观察你的button上面一共有几层才能到你的cell,也就是属一下上面有几个父类才到cell
如图所示,方框里的是button,上面到cell一共有三层。为什么要看有几层,我们来看一下按钮的代码
- (IBAction)enterShopButton:(UIButton *)sender {
&&&&UIView *v = [sender superview];
&&&&UIView *v1 = [v superview];
&&&&UITableViewCell *cell = (UITableViewCell *)[v1 superview];
&&&&NSIndexPath *indexPathAll = [self.tableView indexPathForCell:cell];
&&&&NSLog(@&indexPath:--------%@&,indexPathAll);
首先第一个“v”是获取“View”这一层,然后继续调用superview往上翻(不懂的对照上面的图来看)
“v1”是获取“Content View”这一层,
“cell”就获取到了对应的cell这一层。然后取出cell的路径
path = 2 - 0
“2”代表当前cell所在的section,“0”代表当前cell里的row位置。
再通过“indexPathAll.section”就能获取当前的section了。
个人觉得非常好用,而且很容易理解。
我的热门文章
即使是一小步也想与你分享

我要回帖

更多关于 ios 获取当前点击cell 的文章

 

随机推荐