uiscrollview和uitableview

问题描述:

我有一个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

我不确定你的解决方案究竟发生了什么,但试试这个:在IB和“大小”属性选项卡(标尺)上选择你的UITableView,确保你设置了适当的调整大小选项到边界,高度,宽度规则)

+0

我已检查大小属性。一切安好。问题是当键盘出现在视图上时,表格的大小减小到表格单元格的大小。并且只显示第一个单元格。我通过调整textfieldDelegate的textFieldDidEndEditing:方法中的tableview框架来解决它。我知道这是一个错误的方法,但我没有看到任何其他方式。 – Jayshree 2010-06-22 07:59:06