在单元格按钮上获取tableview行

时间:2014-07-10 13:07:07

标签: ios uitableview didselectrowatindexpath

我有一个带有单元格的tableview(不是自定义),每个单元格都有一个按钮(等等)。

根据单元格,创建的按钮不同。我有3种不同类型的单元格/按钮,评分/查看/编辑(我不确定是否完全相关)

此按钮仅用于此:

[button addTarget:self action:@selector(rateEvent:) forControlEvents:UIControlEventTouchUpInside];

每个按钮的选择器都不同(rateEvent,viewEvent,editEvent)。

这是一个例子,它们都非常相似并且启动了segue序列:

- (IBAction)viewEvent:(UIButton*)sender
{
    [self performSegueWithIdentifier:@"fromHomeToDetails" sender:self];
}

我的didSelectedRowAtIndexPath会在selectedMeeting对象中保存会议的详细信息。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        selectedMeeting = [_nextMeetingsArray objectAtIndex:indexPath.row];
}

segue将selectedMeeting发送给下一个控制器。

我的问题:发送的会议取决于所选的单元格。这意味着用户必须选择一个单元格,然后点击按钮以在下一页上获得正确的详细信息。我该怎么做才能在单元格内获得点击按钮的正确索引路径?

我尝试向viewEvent方法添加参数,使其看起来像-(void)viewEvent:(int)index:(UIButton*)sender;,但它在@selector(viewEvent:)中不起作用。

我能想象但不创造的解决方案是: - 管理以获取点击按钮的单元格的索引路径,并以某种方式将其发送到我的viewEvent方法。 - 创建customCell和/或使用附件,所以我在这里和那里阅读。 - 当我点击按钮时强制行选择,但这也需要知道所述行的索引。

对于(应该?)相当简单的事情,两者似乎都“太复杂”了。我很确定任何有经验的程序员都会有一个我尚未学到的明显简单的答案,我宁愿要求它而不是实现比它应该更重的东西。

另外,如果你对我如何做到这一点有任何评论/批评,我会全力以赴:)

感谢您的时间,一如既往,我非常感激。

编辑:

cellForRow方法:

注意:我删除了与标签有关的所有内容。因为我有不同的按钮,有多个if {}语句,但在我看来它们没有任何影响。此外,该单元基于标签为228的故事板单元;在创建之后设置一个新标记,因此它不应该影响。另外,我在tableview中有50个项目的上限(从webservice设置),因此indexpath无法达到>。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *nextCell = [self.tbvNextMeetings dequeueReusableCellWithIdentifier:@"tbNextCell" forIndexPath:indexPath];
UIButton *bt= (UIButton*) [nextCell viewWithTag:228];
[bt setTitle:@"Edit" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(editEvent:) forControlEvents:UIControlEventTouchUpInside];
[bt setTag:indexPath.row];

return nextCell;

1 个答案:

答案 0 :(得分:1)

在cellForRowAtIndexPath()方法中将标记添加到按钮作为indexpath.row。然后在selector方法中获取sender标签。并传入

NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:yoursection];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

编辑:首先设置标签为228的单元格,然后将其切换为indexpath的值,使其创建失败。

相关问题