Ios 在EditActionsErrorWatIndeXPath中获取indexPath:并在其他地方使用它?
如何从Ios 在EditActionsErrorWatIndeXPath中获取indexPath:并在其他地方使用它?,ios,objective-c,uitableview,swipe,nsindexpath,Ios,Objective C,Uitableview,Swipe,Nsindexpath,如何从editActionsForRowAtIndexPath:获取indexath,并在单独的方法(addSpeciesToFavoriteForCell)中使用该变量 您可以从单元格中获取indexPath,类似于addSpeciesToFavoriteForCell - (void)tableView:(UITableView *)tableView addSpeciesToFavoriteForCell:(UITableViewCell *)cell { NSInd
editActionsForRowAtIndexPath:
获取indexath
,并在单独的方法(addSpeciesToFavoriteForCell
)中使用该变量
您可以从单元格中获取
indexPath
,类似于addSpeciesToFavoriteForCell
- (void)tableView:(UITableView *)tableView addSpeciesToFavoriteForCell:(UITableViewCell *)cell
{
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// other code
}
创建一个实例变量(NSIndexPath)并在EditActionsErrorWatIndeXPath中进行设置
@property (strong, nonatomic) NSIndexPath *index;
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
index = indexPath;
UITableViewRowAction *favAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"Favorite"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self tableView:tableView addSpeciesToFavoriteForCell:cell];
self.tableView.editing = NO;
}];
favAction.backgroundColor = [UIColor greenColor];//color of favorite
return @[favAction];
}
为什么不直接将索引路径传递给方法而不是单元格??而且
tableview
参数也不是必需的,因为它是该类中的一个属性。我这样做了。谢谢。
@property (strong, nonatomic) NSIndexPath *index;
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
index = indexPath;
UITableViewRowAction *favAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"Favorite"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self tableView:tableView addSpeciesToFavoriteForCell:cell];
self.tableView.editing = NO;
}];
favAction.backgroundColor = [UIColor greenColor];//color of favorite
return @[favAction];
}