Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择文本字段时滚动“表格视图顶部”_Ios_Ipad_Uiscrollview_Uitextfield_Uitableview - Fatal编程技术网

Ios 选择文本字段时滚动“表格视图顶部”

Ios 选择文本字段时滚动“表格视图顶部”,ios,ipad,uiscrollview,uitextfield,uitableview,Ios,Ipad,Uiscrollview,Uitextfield,Uitableview,我有一个UITableView自定义单元格,带有UITextField和按钮,我的问题是当用户选择底部keybord中的一些textfield时,会隐藏textfield,我试图通过在stackoverflow中看到一些答案来向上滚动UITableView。但是它不是滚动的。请任何人帮我找出我犯的错误。我已经编写了在textfielddebeginediting:方法中滚动的代码。textfielddebeginediting:也在启动并执行其中的代码,但它没有向上滚动 - (NSInteger

我有一个
UITableView
自定义
单元格
,带有
UITextField
按钮
,我的问题是当用户选择底部
keybord中的一些
textfield
时,会隐藏
textfield
,我试图通过在stackoverflow中看到一些答案来向上滚动
UITableView
。但是它不是滚动的。请任何人帮我找出我犯的错误。我已经编写了在
textfielddebeginediting:
方法中滚动的代码。
textfielddebeginediting:
也在启动并执行其中的代码,但它没有向上滚动

- (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];


}

在滚动tableView之前,必须重新加载它。我在您的文本字段中添加了一行代码。请试试这个

- (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];

}

祝您好运

您应该收听键盘通知并更改表格视图框以显示所需的单元格。滚动是不够的,因为如果单元格位于底部,它将被隐藏

-(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];
}
试试这个

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

参数
动画:是
显示滚动动画。

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

是否-(void)textField didbeginediting:(UITextField*)正在调用textField?尝试此选项是的,正在调用它。他已给出此代码,我应该在何处使用此tableView.contentInset=UIEdgeInsetsMake(0,0,height,0);可能他试图显示的单元格位于tableview的底部,所以滚动不起作用!当我在完成块中给出此代码时//NSArray*indexPathsForVisibleRows=[self.tabelView1 indexPathsForVisibleRows];//NSIndexPath*selectedIndexPath=[indexPathsForVisibleRows对象索引:(indexPathsForVisibleRows.count/2)];//[self.tabelView1 scrollToRowatinexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop动画:否];我得到了不兼容的块指针类型发送'void(^)(void)'到'void(^)(bool)'类型的参数错误我得到了这个错误我得到了不兼容的块指针类型发送'void(^)(void)'到'void(^)(bool)'类型的参数错误抱歉出现了一个错误它应该是完成的:^(bool finished){}它正在向上移动,但是,这里我的桌面视图只是一小部分,它正在穿过视图并移出视图。很遗憾你没有足够的声誉在聊天时讨论它。不管怎样,我已经说过,仅仅滚动对您没有帮助:如果单元格位于表视图的底部,它将不可见。解决方案是缩短表格视图或将其向上移动,然后滚动以使所需单元格可见。这在很大程度上取决于您的布局,所以如果我的解决方案的结果不能解决您的问题,您应该进行试验。滚动后重新加载表数据。
// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];