2012-12-11 66 views
1

我们正在做一个UITableViewCell验证。该电池在点击一个textView和一个自定义按钮,弹出了含有UIDatePickerView应该打开&用户将选择日期。这里选择日期是强制性的。所以我们使用下面的代码进行了验证。 textViewBeginEditing代理方法textView位于紧接着的下一行的单元格内。即当用户点击下一行中的textView时,以前的行值将被验证。 这里验证似乎是工作,但光标仍然闪烁在当前单元格,不移动到正在验证先前的小区。如何设置光标位置的UITextView内的UITableViewCell

UITableViewCell* myCell = (UITableViewCell*)textView.superview ; 

UITextView *txtviewMycelltext;//=[[UITextView alloc]init]; 

for (UIView *view in myCell.subviews) 
{ 

    if([view isKindOfClass:[UILabel class]]) 
    { 

     UILabel *lbl=view; 
     lbl.text=[lbl.text stringByReplacingOccurrencesOfString:@"." withString:@""]; 
     int tablerowindex=[lbl.text intValue]; 
     NSLog(@"%@",lbl.text); 
     break; 
    } 
    if([view isKindOfClass:[UITextView class]]) 
    { 
     txtviewMycelltext=view; 
    } 
} 



inttablerowindex-=1; 

if(inttablerowindex>0) 
{ 
    if([[arrActionvalues objectAtIndex:inttablerowindex1]objectForKey:KEY_FOLLOWUPDATE]==EMPTYSTRING) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] 
             initWithTitle: @"Alert" 
             message: ALERT_FOLLOWUPDATE 
             delegate: nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
     [alert show]; 


//    UITableView *mytable=(UITableView *)((textView.superview).superview).superview; 
     UITableView *myTable=(UITableView*)[[myCell superview]superview]; 

     NSIndexPath *indexPath = (NSIndexPath*)[myTable indexPathForCell: 
      (UITableViewCell*)textView.superview.superview]; 
     UITableViewCell *previousCell = (UITableViewCell*)[myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]]; 

     for (UIView *view in previousCell.subviews) 
     { 
         NSLog(@"%@",view); 
         if([view isKindOfClass:[UIView class]]) { 

          UIView *subView=view; 
          for(UIView *view2 in subView.subviews) 
          { 

           if([view2 isKindOfClass:[UITextView class]]) { 
            UITextView *txt=view2; 

            [txtviewMycelltext resignFirstResponder]; 
            [txt becomeFirstResponder]; 
            break; 
           } 
          } 


         } 
        } 
       } 
      } 

请让我知道需要做什么才能将光标位置移动到正确的单元格。

+0

这是一个情况下我会建议使用标签 CodaFi

+0

究竟是什么,你确认?你的代码主要是通过视图迭代,而不是笨拙的... – Mundi

+0

即时尝试验证用户是否选择了当前单元格中的日期。如果他没有将光标位置设置到他必须选择日期的行。 – user1531912

回答

0

它是不是从你的代码很清楚,但是,我猜你在里面textViewDidEndEditing执行您的验证。

textViewShouldEndEditing是适当的委托方法来执行的文本浏览内容验证。如果您想防止焦点从当前文本视图切换,您可以返回NO。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intfm/UITextViewDelegate/textViewShouldEndEditing

+0

感谢!!!,更改委托方法textViewSHouldBeginEditing代替textViewDidBeginEditing使代码工作按照我们的要求......再次感谢..除此之外,绝对要知道,有没有我们可以将光标移动到任何方式一个控件? – user1531912

+0

'becomeFirstResponder'就是这样做的方法。但是,它通常在委托方法内部不起作用,委托方法本身由响应方更改触发。在您的委托方法中放置一个断点并查看调用堆栈.. – user1885297

相关问题