【问题标题】:Detecting From which Cell , UIButton was pressed in a UITableView检测从哪个 Cell , UIButton 在 UITableView 中被按下
【发布时间】:2015-05-20 13:01:38
【问题描述】:

我有两个自定义 UItableViewCell 和相同的 UIButton ,如何确定从哪个单元格调用 UIButton

附:我不想使用两种不同的方法。

cellForRowAtIndexPath 方法。

if (indexPath.row==1) {
static NSString *cellIdentifier = @“CustomeCell1“;

                            NewsFeedCell1 *cell = (NewsFeedCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}
else
{
static NSString *cellIdentifier = @"CustomeCell2”;

                            NewsFeedCell2 *cell = (NewsFeedCell2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}

按钮点击方法。

-(IBAction)btnPrivacyClicked:(UIButton *)sender
{
    NSLog(@"Privacy Clicked at : %ld",(long)sender.tag);

    // Here Determine From Which Cell UiButton Is Clicked.
    NewsFeedCell1 *cell = (NewsFeedCell1 *)[self.HomeTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];

【问题讨论】:

标签: ios uitableview


【解决方案1】:
// *** Use Following code to detect `Cell` ***
CGPoint buttonPosition = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[self.tableView cellForRowAtIndexPath:indexPath];

【讨论】:

  • 看一下这段代码。它会给 NSIndexPath 的 UiButton。
  • @Naeem 第一行查找给定 UITableView 中的按钮位置,下一行查找屏幕上给定位置的 IndexPath。
【解决方案2】:

为 cell.btnPrivacy.tag=indexPath.row 设置标签;

在按钮操作中,您将获得按钮 btnPrivacy 标签,即 sender.tag,它是单元格索引。

试试这个。

【讨论】:

  • 请仔细阅读问题,我已经将Tag设置为UiButton
  • 我想你后来添加了这个,我在发表评论时无法看到。
  • 是的,我已经编辑了问题,但我的问题是确定从哪个单元格单击 UIButton?
  • 是的,你可以从上面的代码中得到它,只需添加if条件来检查cell1的标签1和其他cell2的其他标签。
  • 我还必须确定行,所以我不能将静态标签分配给 UIButton
猜你喜欢
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多