2

我在表格视图单元格中有一个按钮(名为“deleteTemplate”),当按下它时,我应该得到按钮所在单元格的“index.row”。任何人都知道如何获取单元格的“index.row” ,当您单击单元格中的按钮时?

我当前的按钮代码:

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForCell:clickedCell];

NSDictionary *dic = [tableData objectAtIndex:clickedButtonIndexPath.row];

但是clickedButtonIndexPath = NULL?:(

这在 iOS 6 中有效。

谢谢!!!

4

4 回答 4

5

你得到 null 因为 iOS 7 中的视图层次结构发生了变化。

我推荐使用 Delegate 来获取 TableViewCell 的 indexpath。

您可以从这里获取示例项目

这是示例:

我的视图控制器如下所示:


//#import "ViewController.h"
//#import "MyCustomCell.h"
@interface ViewController () 
{
    IBOutlet UITableView *myTableView;
    NSMutableArray *dataSourceArray;
}
@end
@implementation ViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    dataSourceArray = [[NSMutableArray alloc] init];
    for(int i=0;i<20;i++)
        [dataSourceArray addObject:[NSString stringWithFormat:@"Dummy-%d",i]];
}
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
//pragma mark - UITableView Delegate And Datasource -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdenfifier = @"MyCustomCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdenfifier forIndexPath:indexPath];
    [cell setDelegate:self];
    [cell.myButton setTitle:[dataSourceArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    return cell;
}
//pragma mark - MyCustomCell Delegate -
-(IBAction)myCustomCellButtonTapped:(UITableViewCell *)cell button:(UIButton *)sender
{
    NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
    NSLog(@"indexpath: %@",indexPath);
}
@end

MyCustomCell.h 看起来像这样:


//#import 
@protocol MyCustomCellDelegate 
@optional
- (IBAction)myCustomCellButtonTapped:(UITableViewCell *)cell button:(UIButton *)sender;
@end
@interface MyCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *myButton;
@property(nonatomic, weak)id delegate;
@end

MyCustomCell.m 看起来像这样:


//#import "MyCustomCell.h"
@implementation MyCustomCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    }
    return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}
//#pragma mark - My IBActions -
-(IBAction)myCustomCellButtonTapped:(UIButton *)sender
{
    if([self.delegate respondsToSelector:@selector(myCustomCellButtonTapped:button:)])
        [self.delegate myCustomCellButtonTapped:self button:sender];
}
@end

于 2013-11-10T20:28:46.173 回答
1

您在cellforrowatindexpath中为您的按钮设置标签

clickedButtonIndexPath.tag=indexpath.row;

当按下按钮时写

nslog(@"%i",btutton.tag);

如果 clickedbuttonindexpath 为 nil 它不会 uitableviewcell

于 2013-11-10T17:13:29.407 回答
1

这个问题已经回答了很多次了。为了完成,这是最好的解决方案:

    -(IBAction)cellButtonTapped:(UIButton *)sender
    {
        CGRect buttonFrame = [sender convertRect:sender.bounds toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin];
    }
于 2016-02-20T11:13:15.433 回答
0

当您将单元格分配给您button.tagindexPath.row,当您单击方法中的按钮时,您将拥有发件人(按钮),并且您从发件人的标签中获得 indexPath

于 2013-11-10T17:33:29.487 回答