[英]scroll table view top when selecting an textfield
I Have a UITableView
with custom cell
with UITextField
and buttons
on that ,my issue is when ever the user selects some textfields
in bottom keybord
is hiding that textfield
, i tried to scroll up the UITableView
by seeing some answers in stackoverflow. 我有一个带有自定义
cell
的UITableView
以及UITextField
和buttons
,我的问题是,当用户在底部keybord
选择某些textfields
keybord
,我会隐藏该textfield
,我试图通过在stackoverflow中看到一些答案来向上滚动UITableView
。 but it is not scrolling can any one help me in finding out the mistake made by me please.i have written code for scrolling in textFieldDidBeginEditing:
method . 但这不是滚动,请问谁能帮助我找出我的错误。我已经在
textFieldDidBeginEditing:
method中编写了滚动代码。 textFieldDidBeginEditing:
is also firing and executing the code in that but it is not scrolling up. 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];
}
You should listen to the keyboard notifications and change your table view frame to show the cell you want. 您应该收听键盘通知并更改表格视图框架以显示所需的单元格。 Scrolling isn't enough as if the cell is at the bottom it will be hidden anyway
滚动是不够的,好像该单元格在底部还是会被隐藏
-(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];
}
Try this 尝试这个
[self.myTableView setContentOffset:CGPointZero animated:YES];
OR 要么
// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];
Parameter 参数
animated:YES
show scrolling animation. animated:YES
显示滚动动画。
animated:NO
display table after scrolling(no animation.) animated:NO
滚动后animated:NO
显示表格(不动画)。
You have to reload your tableView before scrolling it. 您必须在滚动之前重新加载tableView。 I have added one line of code in your textFieldDidBeginEditing.
我在您的textFieldDidBeginEditing中添加了一行代码。 PLease try this.
请尝试一下。
- (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];
}
Good Luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.