您好我正在使用tableview.Now我正面临一个问题。在我的tableview中,当我使用Long Gesture时,我需要显示Alert View.it工作正常。当我在警报视图中点击buttonindex 0时,我需要执行一些Task.But,因为我需要indexpath。以下是我执行任务的方法
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(messageDeleteOrForword:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.tableView addGestureRecognizer:lpgr];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self deleteSpecificMessage];
}
if (buttonIndex==1) {
}
}
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
[alert show];
}
-(void)deleteSpecificMessage
{
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
}
但我需要传递一些参数来知道我在下面使用的索引路径 - (无效)deleteSpecificMessage:(ID)发送方 { } 但是如何在alertview中调用和分配参数请帮助我。
答案 0 :(得分:1)
显示didselectrowatindexpath
的提醒视图。将alertview标记设置为此方法的indexpath。通过这种方式,您将使用indexpath集成警报视图。在alertview委托中,您可以将indexpath作为其标记。因此,您可以使用此标记将其删除或转发为考虑索引路径。
例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
alert.tag = indexPath; //setting tag
[alert show];
}
更新(在评论中提问):
你可以这样做,这是一个例子,
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
这里是handleLongpress方法
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
alert.tag = indexPath; //setting tag
[alert show];
}
}
希望这会有所帮助:)
答案 1 :(得分:0)
在你的删除方法中你可以添加:
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
答案 2 :(得分:0)
您可以使用
获取indexpathNSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
您可以查看this link
中的更多详情你也可以在.h文件中加一个IndexPath
变量,在选择tableview单元格时可以分配该变量。
首先添加长按手势识别器。
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionLongPressGeature:)];
[longPressGesture setDelegate:self];
[longPressGesture setMinimumPressDuration:0.3];
[tableView addGestureRecognizer:longPressGesture];
即 选项1
-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:tableViewJobs];
// You need to declare NSIndexPath *indexPathSelected in your .h File
indexPath = [tableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
// Perform your action i.e AlertView
}
}
选项2:
-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
// You need to declare NSIndexPath *indexPathSelected in your .h File
indexPath = [tableView indexPathForSelectedRow];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
// Perform your action i.e AlertView
}
}
以后再使用此变量。
我希望你能找到你想要的东西。
答案 3 :(得分:0)
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else{
NSLog(@"selected row index %ld",(long)indexPath.row);
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
[alert show];
}
}
答案 4 :(得分:0)
您还可以将Objective-C关联对象用于运行时属性。 <objc/runtime.h>
中提供了此功能。例如:
#import <objc/runtime.h>
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
objc_setAssociatedObject(alert, @"currentIndexPath", indexPath, OBJC_ASSOCIATION_RETAIN);
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSIndexPath *idxPath = objc_getAssociatedObject(alertView, @"currentIndexPath");
NSLog(@"%@",idxPath);
if (buttonIndex == 0) {
[self deleteSpecificMessage];
}
if (buttonIndex==1) {
}
}
以下是一些更有帮助的链接。 http://kingscocoa.com/tutorials/associated-objects/ http://nshipster.com/associated-objects/
使用关联对象,您可以使用任何对象附加多个属性并轻松获取。在第一个链接中,您还可以使用定义类别自定义属性。