gpt4 book ai didi

ios - Tableview 按钮获取 index.row

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:55 26 4
gpt4 key购买 nike

我在 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。

谢谢!!!

最佳答案

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

我建议使用 Delegate 来获取 TableViewCell 的索引路径。

You can get the sample project from here

这是示例:

我的 View Controller 看起来像这样:


//#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

关于ios - Tableview 按钮获取 index.row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com