2010-06-21 65 views
0

我有一個scrollview其中包含一個uitableview和幾個按鈕。我已經添加了IB的uitableview。並且表格單元格是動態創建的。我爲每個表格單元格添加了一個標籤和一個文本字段(類似於用戶的輸入表單)。我寫了代碼來在鍵盤出現時調整滾動視圖的大小。所以當我點擊一個文本框時,鍵盤出現並佔用了屏幕的一半,而scrollview則佔據了另一半。問題是,一旦發生這種情況,tableview被改變,只顯示table的第一個單元格。我認爲tableview會被剪切掉。無論我做什麼,tableview都不顯示整個表格......所以我做錯了。uiscrollview和uitableview

繼承人我的代碼。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    scrollView.contentSize=self.view.frame.size; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell= [[tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease]; 
    if (cell==nil) { 

     CGFloat fontSize = [UIFont labelFontSize]-3 ; 
     CGFloat topMargin = floor(0.5 * (tableView.rowHeight - fontSize)) -2; 

     cell = [[[UITableViewCell alloc] 
      initWithFrame:CGRectMake(0, 0, 320, tableView.rowHeight) 
      reuseIdentifier:CellIdentifier] 
      autorelease]; 


    UILabel *labelHeading = [[[UILabel alloc]initWithFrame:CGRectMake(10.0, topMargin, 100.0, fontSize + 4)] autorelease]; 
    labelHeading.text= [[self.menuItems objectAtIndex:indexPath.row] objectForKey:@"itemName"]; 
    [cell addSubview:labelHeading]; 
    txtValue = [[[UITextField alloc] initWithFrame:CGRectMake(120,topMargin,150 ,fontSize + 4)] autorelease]; 
    txtValue.text=[NSString stringWithFormat:@" %@",[dataItems objectForKey:@"name"]]; 
    [cell addSubview:txtValue]; 
    [scrollView addSubview:cell]; 
    } 
return cell; 
} 

所以有人可以幫助我。有沒有想法?

+0

我也有另一個問題。看起來,當鍵盤出現時,整個視圖向右移動一點。這可能是什麼原因。 – Jayshree 2010-06-22 07:44:02

回答

0

我不確定你的解決方案究竟發生了什麼,但試試這個:在IB和「大小」屬性選項卡(標尺)上選擇你的UITableView,確保你設置了適當的調整大小選項到邊界,高度,寬度規則)

+0

我已檢查大小屬性。一切安好。問題是當鍵盤出現在視圖上時,表格的大小減小到表格單元格的大小。並且只顯示第一個單元格。我通過調整textfieldDelegate的textFieldDidEndEditing:方法中的tableview框架來解決它。我知道這是一個錯誤的方法,但我沒有看到任何其他方式。 – Jayshree 2010-06-22 07:59:06

相關問題