[英]UITableView with UITextView scrolling issue
我已經用一些自定義單元格組成了一個tableview。 一些自定義單元包含UIWebView
,一些具有UISlider
加上UITextField
,一些具有UIButton
。
現在,當我單擊UITableView
底部的uitableviewcell子視圖的文本字段時, 我無法設法使表格單元格在鍵盤上方正確可見。
我無法使用UITableViewController
而不是UIViewController
。 因為我的UI結構有些奇怪和動態。 簡單來說就像
UIView -> UIScrollView with paging -> x number of UITableViews -> y number of UITableViewCell -> z number of UITextFields, UISliders, UIWebViews, UIButtons, UILables, etc.
我還嘗試了下面的代碼以使其工作。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
但它不起作用:(
謝謝,
編輯1:
我已經檢查了文本字段,tableviewcell和tableview的引用。 因此,引用對象不是問題。
試試這個代碼。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
NSIndexPath *indexPath = [[table_Question[pageNumber] indexPathForCell:cell];
int totalRows = 0;
if([[table_Question[pageNumber] numberOfSections] > 1)
{
for(int i = 0; i<([indexPath.section] - 1);i++)
{
totalRows += [[table_Question[pageNumber] numberOfRowsInSection:i];
}
}
totalRows += indexPath.row;
int yPosition = totalRows * cell.frame.size.height;
CGPoint offset = CGPointMake(0, yPosition);
[[table_Question[pageNumber] setContentOffset:offset animated:YES];
}
這對您有幫助...
根據我的理解,當用戶開始在UITableViewCell
內部編輯UITextField
時,您希望顯示UITextField
和鍵盤。 如果是這種情況,可以將UITableView
的contentInset
設置為所需的大小,然后滾動到所需的索引路徑。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
@try
{
table_Question[pageNumber].contentInset = UIEdgeInsetsMake(CGFloat Req_top, CGFloat Req_left, CGFloat Req_bottom, CGFloat Req_right);
[table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//Add this to you code to scroll in the tableview gets hidden by keyboard
CGPoint scrollPoint = CGPointMake(0, Required_Height);
[yourScrollView setContentOffset:scrollPoint animated:YES];
}
}
試試這個代碼
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *aCell = [table_Question[pageNumber] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
CGSize cellSize = aCell.frame.size;
[table_Question[pageNumber] setContentOffset:CGPointMake(0, cellSize.height*2) animated:YES];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.