我在tableview单元格中有一个按钮(名为“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。
谢谢!!!
答案 0 :(得分:5)
您将获得null,因为iOS 7中的视图层次结构已更改。
我建议使用Delegate获取TableViewCell的索引路径。
You can get the sample project from here
以下是样本:
我的视图控制器如下所示:
//#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
答案 1 :(得分:1)
在 cellforrowatindexpath
中为按钮设置标记clickedButtonIndexPath.tag=indexpath.row;
当按钮处于动作写入时
nslog(@"%i",btutton.tag);
如果clickedbuttonindexpath为nil则不会uitableviewcell
答案 2 :(得分:1)
这个问题已经多次回答了。完成后,这是最佳解决方案:
-(IBAction)cellButtonTapped:(UIButton *)sender
{
CGRect buttonFrame = [sender convertRect:sender.bounds toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin];
}
答案 3 :(得分:0)
当您为button.tag
indexPath.row
分配单元格时,当您单击方法中的按钮时,您有发件人(按钮)并从发件人的标签中获取indexPath