0

我有一个UITableView自定义cellUITextFieldbuttons我的问题是当用户textfields在底部选择一些keybord隐藏的时候,我试图通过在stackoverflow中看到一些答案textfield来向上滚动。UITableView但它不是滚动任何人都可以帮助我找出我犯的错误。我已经编写了用于滚动textFieldDidBeginEditing:方法的代码。textFieldDidBeginEditing:也在触发并执行其中的代码,但它没有向上滚动。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
   // NSLog(@"No OF rows:%d",[contents count]);
return [contents count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{

static NSString *cellIdentifier = @"cell";

// Try to retrieve from the table view a now-unused cell with the given identifier.
cell = (uploadCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"uploadCustomCell"];
if (cell == nil) {
    cell = [[uploadCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"uploadCustomCell"];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"uploadCustomCell"
                                                        owner:self options:nil];
          cell = [nib objectAtIndex:0];
}


saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
dictionaryContents = [contents objectAtIndex:indexPath.row];
cell
.nameLabelCell.text   = [dictionaryContents valueForKey:@"VideoName"];
cell.userName.text = [dictionaryContents valueForKey:@"User"];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
   NSString *defaultVideo = [dictionaryContents valueForKey:@"DefaultVideo"];
if ([defaultVideo isEqual: @"1"]) {
    [defaultSwitche setOn:YES animated:YES];

}
else{
    [defaultSwitche setOn:NO animated:YES];
}


[cell.defaultSwitch addTarget:self action:@selector(setState:)forControlEvents:UIControlEventValueChanged];
VideoNameTextField.hidden = YES;
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 207;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    selectedRow=indexPath.row;
indexpathTest = indexPath.row;
[tabelView1 reloadData];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
guid = [dictionary valueForKey:@"GUID"];
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;


}
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath  *)indexPath
 {
  return YES;
 }
  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  return  UITableViewCellEditingStyleDelete;

}

 - (void)textFieldDidBeginEditing:(UITextField *)textField {



[self.tabelView1 scrollToRowAtIndexPath:1 atScrollPosition:UITableViewScrollPositionTop animated:YES];


}
4

3 回答 3

0

您应该听取键盘通知并更改表格视图框架以显示您想要的单元格。滚动是不够的,就像单元格在底部一样,无论如何它都会被隐藏

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillDisappear:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

-(void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)keyboardWillAppear:(NSNotification *)note
{
    CGRect tableViewFrame = self.tableView.frame;
    // If your table view doesn't end at the bottom of the screen then this calculation of the height wouldn't be enough, you would need to take into consideration the distance between the screen bottom and the table view
    tableViewFrame.size.height = tableViewFrame.size.height - [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    CGRect visibleFrame = CGRectZero;
    visibleFrame.origin = self.tableView.contentOffset;
    visibleFrame.size = tableViewFrame.size;

    [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                     animations:^{
                         self.tableView.frame = tableViewFrame;
                     }
                     completion:^(BOOL finished){
                         [self.tableView scrollRectToVisible:visibleFrame
                                                    animated:YES];
        }];
}

-(void)keyboardWillDisappear:(NSNotification *)note
{
    CGRect tableViewFrame = self.tableView.frame;
    tableViewFrame.size.height = tableViewFrame.size.height + [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                     animations:^{
                         self.tableView.frame = tableViewFrame;
                     }
                     completion:nil];
}
于 2013-05-20T09:24:12.280 回答
0

尝试这个

[self.myTableView setContentOffset:CGPointZero animated:YES];

或者

// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];

参数
animated:YES显示滚动动画。
animated:NO 滚动后显示表格(无动画。)

于 2013-05-20T10:15:52.570 回答
-2

您必须在滚动之前重新加载您的 tableView。我在您的 textFieldDidBeginEditing 中添加了一行代码。请试试这个。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    [self.tabelView1 reloadData];

    NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows];
    NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)];
    [self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

}

祝你好运

于 2013-05-20T09:05:36.550 回答