Tableviewbutton获取index.row
我在一个tableview单元格(名为“deleteTemplate”)中有一个button,当它被按下时,我应该得到button所在单元格的“index.row”。任何人都知道如何获得单元格的“index.row” ,当你点击单元格中的一个button?
我目前的button代码:
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForCell:clickedCell]; NSDictionary *dic = [tableData objectAtIndex:clickedButtonIndexPath.row];
但是clickedButtonIndexPath = NULL? 🙁
这在iOS 6中工作。
谢谢!!!
由于iOS 7中的视图层次结构已更改,因此您将得到空值。
我build议使用Delegate获取TableViewCell的索引path。
你可以从这里得到示例项目
这是样本:
我的视图控制器看起来像这样:
//#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
这个问题已经回答了很多次了。 为了完成,这里是最好的解决scheme:
-(IBAction)cellButtonTapped:(UIButton *)sender { CGRect buttonFrame = [sender convertRect:sender.bounds toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin]; }
当你让单元格在你button.tag
indexPath.row
分配,当你点击你的方法中的button,你有发件人(button),你从发件人的标签索引path
您在cellforrowatindexpath中为您的button设置标签
clickedButtonIndexPath.tag=indexpath.row;
当button被按下时,写入
nslog(@"%i",btutton.tag);
如果clickedbuttonindexpath是零,它不会uitableviewcell