![](/img/trans.png)
[英]In a UITableView, how do I know when scrollRectToVisible is complete for a row?
[英]How do I know when a UITableView looses focus?
我有一个UITableView每行都有一个UITextField。 当用户在表格视图之外触摸时,取消选择文本字段,我想调用一个方法。
但是,如果用户选择表的另一行,我不想调用这种方法。
谢谢
** alloc_iNit的代码**:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textField] setTag:[indexPath row]];
[[cell textField] setDelegate:self];
NSString *tagName = [tagsDisplayName objectAtIndex:[indexPath row]];
[[cell textField] setText:tagName];
return cell;
}
需要考虑的是在UITableView后面的视图上使用Tap Gesture识别器。 您可能需要处理shouldReceiveTouch事件(在手势识别器和按钮操作中进行了详细说明),以防止单击UITableView中的某个位置时触发Tap Gesture Recognizer。
您必须在实际的UITableView自身上覆盖hitTest:withEvent:。 请记住,它控制了响应者链,因此子视图将没有机会首先处理它,除非我们显式覆盖该行为
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { return [super hitTest:point withEvent:event]; }
hitTest:withEvent:负责告诉系统哪个命中了视图,默认情况下,UITableView假定自己(或其单元格之一),因此您必须弄清楚用户是否触摸了位置,以及用户是否触摸了表格视图,因此返回该视图代替。
修改后的代码:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"hitTest"); UIView *subview = [super hitTest:point withEvent:event]; if (event.type == UIEventTypeTouches) { // get touches NSSet *touches = [event allTouches]; NSLog(@"subview: %@", subview); NSLog(@"touches: %@", touches); } }
第一建议:
我相信系统会对此有所帮助,因为它并没有帮助我(我必须解决此问题)。 如果您有两个文本字段,并且有一个打开的文本框,然后点击另一个文本框,则第二个文本框会
textFieldDidShouldBeginEditing:
在发送原始文件之前:
textFieldDidEndEditing: // or textFieldShouldEndEditing
将几个日志消息添加到您的项目中以验证此消息,如果不是,则可能是其他消息。 一年前,由于明显的混乱,我遇到了问题。
2012-08-30 09:22:40.528 Searcher[22053:f803] SHOULD BEGIN // first tap on first textField
2012-08-30 09:22:40.534 Searcher[22053:f803] BEGIN
2012-08-30 09:22:42.168 Searcher[22053:f803] SHOULD BEGIN // second tap on second TF
2012-08-30 09:22:42.168 Searcher[22053:f803] SHOULD END
2012-08-30 09:22:42.170 Searcher[22053:f803] END
第二建议:
用户可以在任何地方点击视图,但是如果他们点击相同的文本字段(以获取复制/粘贴),则不想关闭。
创建一个新的ivar,当它获取'textFieldDidShouldBeginEditing'时存储textField:
__block UITextField * currTextField;
将透明视图置于您的视图(键盘除外)上,以检测触摸事件
当透明视图看到touch事件时,如果未设置textField或触摸位于原始touchView内,则不执行任何操作
如果触摸在其他地方,并且有currentTextField,则转发事件,然后按如下所示将块分配给mainQueue:
UITextField * lastTextField = currTextField; dispatch_async(dispatch_get_main_queue(),^ {if(currTextField && currTextField == lastTextField){[currTextField resignFirstResponder]; //触摸到textField的外部,但是没有新的}}));
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(activeTextField) //declare it in .h file
[self textFieldDidEndEditing:activeTextField];
activeTextField = textField;
CGRect textViewRect = [tableView convertRect:activeTextField.bounds fromView:activeTextField];
[tableView scrollRectToVisible:textViewRect animated:NO]; //if u want to add scroll to that cell
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeTextField = nil;
}
然后你可以使用touchesBegan
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[activeTextField resignFirstResponder];
}
希望这对您有所帮助。 快乐的编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.