带有 UIButton 的自定义 UITableView 单元格不调用 didSelectRowAtIndexPath [英] Custom UITableView Cell with UIButton not Calling didSelectRowAtIndexPath

查看:29
本文介绍了带有 UIButton 的自定义 UITableView 单元格不调用 didSelectRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能会发生,但我无法找到在点击按钮时调用此方法的方法 方法被调用但选择了错误的单元格.

I understand this is suppose to happen but I haven't been able to find a way to call this method when the button is tapped The method gets called but the wrong cell is selected.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
_postCell = (postCell *) [self.tableView dequeueReusableCellWithIdentifier:@"postCell"];
_postCell.personStringPost.text = [object objectForKey:@"stringPost"];
[_postCell.nameLabel setTitle:[object objectForKey:@"User_Name"]  forState:UIControlStateNormal];
_postCell.userId = [object objectForKey:@"userId"];
_postCell.selectionStyle = UITableViewCellSelectionStyleNone;

PFFile *imageFile = [object objectForKey:@"profileImage"];
NSData *data = [imageFile getData];
_postCell.profileImage.image = [UIImage imageWithData:data];

[_postCell.nameLabel addTarget:self action:@selector(personProfile:tableView:) forControlEvents: UIControlEventTouchUpInside];
[_postCell.profileImageButton addTarget:self action:@selector(personProfile:tableView:) forControlEvents:UIControlEventTouchUpInside];

    return _postCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
postCell *cell = (postCell *)[tableView cellForRowAtIndexPath:indexPath];
self.userId = cell.userId;
NSLog(@"Did Select: %@", self.userId);
}

- (void) personProfile: (NSIndexPath *) indexPath tableView: (UITableView *) tableView{
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
 [self performSegueWithIdentifier:@"personProfile" sender:self];
}

推荐答案

我希望它能帮到你

同时处理只有一节的按钮

while dealing with button with only one section

in cellfor row 方法

in cellfor row method

yourcell.mybutton.tag=indexPath.row;

-(void)myCellbuttonAction:(id)sender
{
   UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
  NSInteger row = button.tag; // recover the row
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];

  // apply your logic for indexpath  as in didselect row   
}

在处理多个部分和多行时,这可能会对您有所帮助

While dealing with multiple sections and multiple rows it this might help you

在行方法的单元格中

yourcell.tag=indexPath.section;
yourcell.contentview.tag=indexPath.row;

您的按钮操作可能如下所示

and your button action might look like this

-(void)myCellbuttonAction:(id)sender
{
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton

id rowid =[button superview];
id sectionid = [rwoid superview];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[rowid tag] inSection:[sectionid tag]];

 // apply your logic for indexpath  as in didselect row   
}

这篇关于带有 UIButton 的自定义 UITableView 单元格不调用 didSelectRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆