2011-07-12 3 views
4

UITableViewをUITableViewCellに埋め込みましたが、ほとんどのテーブルが正常に動作しています。しかし、UITextViewで編集を有効にして編集を開始すると、テーブルが大きくずれてカーソルが不明瞭になります。テーブルビューのスクロール距離を制御する方法はありますか?ビューをセルに手動でスクロールしますが、スクロールしてビューに戻す前にビューをスクロールします。埋め込みUITextViewの編集時にUITableViewがスクロールしすぎる

ここに私が意味する例があります: UITextFieldを編集する - フィールドはキーボードの上にきれいに置かれます。 UITextViewを編集http://imageshack.us/photo/my-images/13/textfield.png/

- フィールドは、キーボードからツールバーを削除大幅にキーボードの上に配置され、何も影響しません) http://imageshack.us/photo/my-images/809/textview.png/

ここでのUITableViewControllerオブジェクトにUITextViewに関連するすべてのコードがあります:

- (void)viewDidLoad 
{ 
    [super viewDidLoad];  

    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    [[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(keyboardWillShow:) 
               name: UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(keyboardWillHide:) 
               name: UIKeyboardWillHideNotification object:nil]; 
} 

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 
    viewTargetedForEditing = (UIView *)textView; 
    return YES; 
} 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    [self.navigationController setNavigationBarHidden:YES animated:YES]; 


    // shrink the textView to fit on-screen 
    originalTextViewFrame = textView.frame; 
    CGRect keyboardRect = [[keyboardUserinfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGFloat keyboardHeight = keyboardRect.size.height; 


    CGRect newViewFrame = textView.frame; 
    CGFloat spaceAboveKeyboard = self.containerView.frame.size.height - keyboardHeight; 
    newViewFrame.size.height = (spaceAboveKeyboard < textView.frame.size.height) ? spaceAboveKeyboard : textView.frame.size.height; 


    /* animate the calculations */ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:[[keyboardUserinfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 

    textView.frame = newViewFrame; 

    [UIView commitAnimations]; 



    // recalculate the keyboard height, in case we aren't in portrait mode 
    CGFloat toolbarHeight; 
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { 
     toolbarHeight = 44.0; 
    } else { 
     toolbarHeight = 32.0; 
    } 

    CGRect frame = textView.inputAccessoryView.frame; 
    frame.size.height = toolbarHeight; 
} 

- (void)endTextViewEditing 
{ 
    [viewTargetedForEditing resignFirstResponder]; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    [self.navigationController setNavigationBarHidden:NO animated:YES]; 
} 

#pragma mark - Keyboard Notifications 

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    [keyboardUserinfo release]; 
    keyboardUserinfo = [[notification userInfo] retain]; 
    [self viewDidBeginEditing:viewTargetedForEditing]; 
} 
- (void)keyboardWillHide:(NSNotification *)notification 
{ 
    [keyboardUserinfo release]; 
    keyboardUserinfo = [[notification userInfo] retain]; 
    [self viewDidEndEditing:viewTargetedForEditing]; 
} 

- (void)viewDidBeginEditing:(id)aView 
{ 
    // called 2nd 

    UITableViewCell *cell = (UITableViewCell *)[aView superview]; 
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 
- (void)viewDidEndEditing:(id)aView 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[viewTargetedForEditing superview]]; 

    gallupAppDelegate *appDelegate = (gallupAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    switch (indexPath.section) { 
     case SectionDescription: 
      if (![[(UITextField *)aView text] isEqualToString:self.plan.Description]) { 
       self.plan.Description = [(UITextField *)aView text]; 
       [appDelegate saveManagedContext]; 
      } 
      break; 

     case SectionNotes: 
      if (![[(UITextView *)aView text] isEqualToString:self.plan.Notes]) { 
       self.plan.Notes = [(UITextView *)aView text]; 
       [appDelegate saveManagedContext]; 
      } 
      break; 

     case SectionLocation: 
      if (![[(UITextField *)aView text] isEqualToString:self.plan.Location]) { 
       self.plan.Location = [(UITextField *)aView text]; 
       [appDelegate saveManagedContext]; 
      } 
      break; 
    } 
} 

TextViewCellはUITableViewCellのサブクラスであり、それは私にscrolling.Hereの実装に問題を与えている細胞である。

@implementation TextViewCell 

@synthesize contents=_contents; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _contents = [[UITextView alloc] initWithFrame:CGRectZero]; 
     [self addSubview:_contents]; 
     self.contents.font = [UIFont systemFontOfSize:17]; 
     self.contents.dataDetectorTypes = UIDataDetectorTypeAll; 


     CGRect frame = CGRectMake(0, 0, self.window.frame.size.width, 44.0); 
     UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
     UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(endTextViewEditing)]; 
     UIToolbar *textViewToolbar = [[UIToolbar alloc] initWithFrame:frame]; 
     textViewToolbar.barStyle = UIBarStyleBlackOpaque; 
     [textViewToolbar setItems:[NSArray arrayWithObjects:spacer, done, nil]]; 

     self.contents.inputAccessoryView = textViewToolbar; 
    } 
    return self; 
} 

- (void)endTextViewEditing 
{ 
    [self.contents resignFirstResponder]; 
} 

#define LEFT_MARGIN 15 
#define TOP_MARGIN 5 
- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    CGFloat width = self.frame.size.width - (LEFT_MARGIN *2); 
    CGFloat height = self.frame.size.height - (TOP_MARGIN *2); 
    self.contents.frame = CGRectMake(LEFT_MARGIN, TOP_MARGIN, width, height); 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    //[super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 
+0

スクリーンショットを追加して、コードを追加してください。 –

+0

私はいくつかのスクリーンショットを追加しました。できるだけ早くコードを追加します。私はたいていかなり手際が良いですが、私がすぐに困惑している理由は、自分のコードでスクロールすることに触れないことです。私は、この問題でこのような犠牲になり、私が直面しているのと同じ困難を克服した人を見つけようとしていました。私はあなたの助けを楽しみにしています(すぐに私はいくつかの一貫性のある関連するコードを掲載することができます) – JoBu1324

+0

私はあなたのコードを持っている! – JoBu1324

答えて

2

結局のところ、のUITableViewをサブクラス化し、次の固定問題の実装:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated 
{ 
    [super setContentOffset:contentOffset animated:animated]; 
    return; 



    static int i = 0; 
    if (0 == i) { 
     i++; 
    } else { 
     i = 0; 
     [super setContentOffset:contentOffset animated:animated]; 
    } 
    return; 
} 

どうやら-setContentOffset:アニメーション:最初の1の実行が許可されている場合は、メッセージが2倍を送った、とされたが2回目のラウンドでは、適切に仕事をしません。

これは問題を解決する理由は誰にもありませんか?

+0

最初のreturn文は意図的ですか? – stepmuel

+0

私はiPhone SDKを使って知っていたので、長すぎました。両方の方法でコードを試して、答えを更新してください!確かに私はreturn文を見落としたように見えます。 – JoBu1324

関連する問題