如何跟踪表视图单元格中按钮的索引路径? [英] How can I keep track of the index path of a button in a table view cell?

查看:137
本文介绍了如何跟踪表视图单元格中按钮的索引路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图,其中每个单元格都有一个按钮附件视图。该表由读取的结果控制器管理,并且经常重新排序。我想能够按下其中一个按钮并获得该按钮的表视图单元格的索引路径。我一直试图让这个工作几天,通过存储按钮的行在其标签,但是当表被重新排序,行会变得不正确,我不断正确地重新排序的标签失败。关于如何跟踪按钮的单元格的索引路径的任何新想法?

I have a table view where each cell has a button accessory view. The table is managed by a fetched results controller and is frequently reordered. I want to be able to press one of the buttons and obtain the index path of that button's table view cell. I've been trying to get this working for days by storing the row of the button in its tag, but when the table gets reordered, the row becomes incorrect and I keep failing at reordering the tags correctly. Any new ideas on how to keep track of the button's cell's index path?

推荐答案

我创建了一个获取indexPath的方法这将帮助你。

I have created one Method for getting indexPath, Hope this will help you.

在cellForRowAtIndexPath中创建按钮操作(aMethod :)

Create Button Action (aMethod:) in cellForRowAtIndexPath

-(void) aMethod:(UIButton *)sender
{
    // Calling Magic Method which will return us indexPath.
    NSIndexPath *indexPath = [self getButtonIndexPath:sender];

    NSLog(@"IndexPath: %li", indexPath.row);
    NSLog(@"IndexRow: %li", indexPath.section);
}

//这是用于获取按钮的indexPath的魔法方法

// Here is the Magic Method for getting button's indexPath

-(NSIndexPath *) getButtonIndexPath:(UIButton *) button
{
    CGRect buttonFrame = [button convertRect:button.bounds toView:groupTable];
    return [groupTable indexPathForRowAtPoint:buttonFrame.origin];
}

这篇关于如何跟踪表视图单元格中按钮的索引路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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