简体   繁体   English

如何在CustomCell按钮中检测indexPath.row单击?

[英]How to detect indexPath.row in CustomCell button Click?

I have a custom cell. 我有一个自定义单元。 like this 像这样

细胞

There are buttons, like LIKE, COMMENT, SHARE. 有一些按钮,例如LIKE,COMMENT,SHARE。

I want indexPath.row in method when click these buttons. 单击这些按钮时,我需要indexPath.row方法。

I made protocol like this 我做了这样的协议

@protocol Cell_StreamCellDelegate
@required

-(void)like;
-(void)comment;
-(void)share;


@end

I called them when methods in CELL.m like as below 我在CELL.m中的方法如下时调用了它们

-(IBAction)likePressed:(id)sender{
    [delegate like];
}
-(IBAction)commentPressed:(id)sender{
    [delegate comment];
}
-(IBAction)sharePressed:(id)sender{
    [delegate share];
}

But I am unable to fetch the indexPath.row. 但是我无法获取indexPath.row。 I want get an object which is on particular index. 我想要一个在特定索引上的对象。

Thanks 谢谢

In your delegate methods you could send the cell that called the methods (ie self ) 在您的委托方法中,您可以发送调用方法的单元格(即self

Something like: 就像是:

- (void)commentForCell:(UITableViewCell *)cell;

When you call the delegate method do it like 当您调用委托方法时,请像

[delegate commentForCell:self];

Then when you implement the delegate method 然后当您实现委托方法时

- (void)commentForCell:(UITableViewCell *)cell
{
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
    ...
}

It is very common to use the caller as a param in a delegate method, as you can see in any standard SDK protocol (collection view, table view, alert view...) 正如在任何标准SDK协议(集合视图,表视图,警报视图...)中所看到的,在委托方法中将调用方用作参数是很常见的。

Declare a new integer property as row in custom cell subclass then assign its value as indexpath.row in cellForRowAtIndex method. 在自定义单元格子类中将新的整数属性声明为行,然后在cellForRowAtIndex方法中将其值分配为indexpath.row。

customcell.row = indexpath.row.

Use this row in protocol 在协议中使用此行

 [delegate like:row]

Try this simple one , Just converting the touched points into the cell Index : 试试这个简单的方法,只需将touched points into the cell Index

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:touchPoint];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM