2014-10-05 232 views
1

我明白这是假设发生,但我一直没有找到一种方法来调用此方法时轻按按钮方法被调用,但选择了错误的单元格。自定义的UITableView单元格与UIButton不调用didSelectRowAtIndexPath

- (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]; 
} 

回答

2

几件事我希望这将有助于

在处理按钮只有一个部分

在cellfor排方法

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 
} 

当处理多个节和多行它这可能帮助你

单元格中的行方法

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

和您的按钮操作可能是这样

-(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 
} 
1

这里有几件事。

a)我不会在你的cellForRowAtIndex方法中使用实例变量(_postCell)。您可能会遇到单元重新使用问题,这可能是您错误的根源。与局部变量替换为:

postcell *cell = (postCell *)[self.tableView dequeueReusableCellWithIdentifier:@"postCell"]; 

您还需要与cell全部更换引用_postCell

b)请注意,在同一行中,您的演员阵容使用小写字母= (postCell *)... - 我已经完成了上述操作,但对于以大写字母开头的类名来说,这是最佳做法。

c)你有一个名为nameLabel财产,这表明它是一个UILabel,但您使用的是setTitle:forState:方法,这意味着它是一个UIButton。我会重命名这个属性,因为如果名称与类匹配(或者至少不暗示错误的类),调试将变得容易很多。

d)当您拨打addTarget:action:forControlEvents方法时,您的选择器为personProfile:tableView:。该方法的签名用于NSIndexPath和UITableView。但是你的按钮不会发送那些类型的参数。它会发送发件人的详细信息 - 即触发该操作的按钮。所以,你需要修改你的方法接受类型的参数:

[cell.nameLabel addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside]; 

e)如果该方法被调用,您需要一种方法来确定发送按钮是哪个小区。理想情况下,您可以将UIButton子类添加到单元格的某个链接,但是(如果您只有一个部分),您可能会将行号作为标记离开。要做到这一点附加:

cell.nameLabel.tag = indexPath.row; 

cellForRowAtIndexPath:。在上面的,你不应该呼叫tableView:didSelectRowAtIndexPath:

-(void)buttonPressed:(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]; // derive the indexPath, assuming section is 0 
    [self.tableView selectRowAtIndexPath:indexPath animated:YES]; // select the relevant row in the table (assuming the table is self.tableView) 
    [self performSegueWithIdentifier:@"personProfile" sender:self]; // perform the segue 
} 

F)注:然后你就可以实现不同的方法来处理按下按钮,如下所示。这是为了在用户选择行时调用tableView,而不是以编程方式选择行。改为使用selectRowAtIndexPath:animated:

+0

这没有奏效。没有错误信息,只是没有工作 – Peter 2014-10-06 02:13:23

+0

没有工作没有错误信息 – Peter 2014-10-06 03:47:08

+0

究竟什么不行?选择了错误的单元格吗?没有选择任何单元格? didSelectRowAtIndex被调用了吗?把一些NSLogs放在:'buttonPressed:'被调用?它是否在'selectRowAtIndexPath'中使用了正确的indexPath? – pbasdf 2014-10-06 08:00:36

0

这基本上是pbasdf与斯威夫特3

上述回答我打电话didSelectRowAt直接,因为它不是由selectRowAtIndexPath触发

该标记设置在单元格创建时使用索引路径的行

getTableView功能是我自己

@IBAction func actionButtonPushed(_ sender: Any) { 
    guard let button = sender as? UIButton else { return } 
    guard let tableView = getTableView() else { return } 
    let indexPath = IndexPath(row: button.tag, section: 0) 
    tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) 
    if let delegate = tableView.delegate { 
     delegate.tableView!(tableView, didSelectRowAt: indexPath) 
    } 
} 
相关问题