我尝试打开一个 UIAlertView
,其中有两个 textfield
以及我选择的单元格中的文本。我使用这段代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer requireGestureRecognizerToFail:tapRecognizer];
CardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
NSString *cellFirstText = cell.cellFirstText.text;
NSString *cellSecondText = cell.cellSecondText.text;
[[NSUserDefaults standardUserDefaults] setObject:cellFirstText forKey:@"cellFirstToEdit"];
[[NSUserDefaults standardUserDefaults] setObject:cellSecondText forKey:@"cellSecondToEdit"];
}
-(void)edit: (UITapGestureRecognizer *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"save", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:1] setSecureTextEntry:NO];
[alert textFieldAtIndex:0].text = [[NSUserDefaults standardUserDefaults] objectForKey:@"cellFirstToEdit"];
[alert textFieldAtIndex:1].text = [[NSUserDefaults standardUserDefaults] objectForKey:@"cellSecondToEdit"];
alert.tag = 3;
[alert show];
}
但问题是,即使我在该行上单击(ones),文本也会更改为默认文本。我希望你明白我的意思。
最佳答案
我认为您的代码是错误的,因为您在每次用户点击单元格时创建 UITapGestureRecognizer,而不是在单元格加载时创建,并且您将其分配给 View ,而不是单元格。
那么试试这个:
移动这段代码:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer requireGestureRecognizerToFail:tapRecognizer];
cellForRowAtIndexPath 方法初始化单元格后,将 [self.view addGestureRecognizer:tapRecognizer];
更改为 [cell addGestureRecognizer:tapRecognizer];
因为你想要将手势添加到单元格而不是 View 。
我不知道这是否真的能解决您的问题,但我认为您的代码中有两处错误。理论上它应该有效。
编辑:
同时尝试将警报初始化消息设置为 nil
。另外,真的有必要使用 NSUserDefaults 来存储这些变量吗?不能将它们仅存储在两个 NSString 属性中,还是您出于某种原因保留了这些变量?
关于ios - 双击 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230984/