【问题标题】:scroll table view top when selecting an textfield选择文本字段时滚动表格视图顶部
【发布时间】:2013-05-14 19:04:44
【问题描述】:

我有一个UITableView 和自定义cellUITextFieldbuttons ,我的问题是用户在底部keybord 中选择一些textfields 时隐藏了textfield,我试图通过在stackoverflow中查看一些答案来向上滚动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];


}

【问题讨论】:

标签: ios ipad uiscrollview uitextfield uitableview


【解决方案1】:

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

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

【讨论】:

  • 当我在完成块中给出这个代码时 // NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows]; // NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)]; // [self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop 动画:NO];我收到不兼容的块指针类型将“void (^)(void)”发送到“void (^)(bool)”类型的参数错误
  • 我收到此错误我收到不兼容的块指针类型将“void (^)(void)”发送到“void (^)(bool)”类型的参数错误
  • 抱歉出现错误应该是完成:^(BOOL finished){}
  • 它正在向上移动,但是,这里我的表格视图只有一小部分只是它穿过视图并移出视图。
  • 很遗憾您没有足够的声望通过聊天进行讨论。无论如何,正如我已经说过的那样,单独滚动对您没有帮助:如果单元格位于表格视图的底部,它将不可见。解决方案是使表格视图变短或向上移动,然后滚动以使您想要的单元格可见。这在很大程度上取决于您的布局,因此如果我的解决方案的结果不能解决您的问题,您应该进行试验。
【解决方案2】:

试试这个

[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滚动后显示表格(无动画)

【讨论】:

  • 滚动后重新加载表格数据。
【解决方案3】:

您必须在滚动之前重新加载您的 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];

}

祝你好运

【讨论】:

  • 可能他试图显示的单元格位于 tableview 的底部,所以滚动不起作用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多