Tableview按钮获取index.row [英] Tableview button get index.row
问题描述
我在tableview单元格中有一个按钮(名为deleteTemplate),当它被按下时,我应该得到按钮所在单元格的index.row。任何人都知道如何获取index.row对于单元格,单击单元格中的按钮?
I got a button in a tableview cell (named "deleteTemplate"), and when it's pressed i should get the "index.row" for the cell the button is in. Anybody knows how to get the "index.row" for the cell, when you click a button in the cell?
我当前的按钮代码:
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForCell:clickedCell];
NSDictionary *dic = [tableData objectAtIndex:clickedButtonIndexPath.row];
但clickedButtonIndexPath = NULL? :(
But clickedButtonIndexPath = NULL? :(
这适用于iOS 6。
谢谢!!!
推荐答案
由于iOS 7中的视图层次结构已更改,因此您将获得null。
You are getting null because the view hierarchy in iOS 7 is changed.
我建议使用使用Delegate获取TableViewCell的索引路径。
I recommend to use Delegate for getting the indexpath of TableViewCell.
以下是示例:
我的视图控制器如下所示:
My View Controller looks like this:
//#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如下所示:
MyCustomCell.h looks like this:
//#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如下所示:
MyCustomCell.m looks like this:
//#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
这篇关于Tableview按钮获取index.row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!