Ios 如果单元格数量少于一个屏幕,则滚动UITableView
我在每个单元格中都有Ios 如果单元格数量少于一个屏幕,则滚动UITableView,ios,objective-c,uitableview,scroll,Ios,Objective C,Uitableview,Scroll,我在每个单元格中都有UITableView和UITextfield。在本场景开始时,我在tableView中有一个单元格,当我开始键入时,单元格数量会增加: 若在这个位置我将再添加一个单元格,它将被键盘重叠,所以我需要滚动tableView以保持当前文本字段的焦点。我尝试了-scrollToRowAtIndexPath:atScrollPosition:animated:,但看起来只有当单元格数量超过一个屏幕所能容纳的数量时(不确定是否为tableView内容Inset
UITableView
和UITextfield
。在本场景开始时,我在tableView中有一个单元格,当我开始键入时,单元格数量会增加:
若在这个位置我将再添加一个单元格,它将被键盘重叠,所以我需要滚动tableView以保持当前文本字段的焦点。我尝试了-scrollToRowAtIndexPath:atScrollPosition:animated:
,但看起来只有当单元格数量超过一个屏幕所能容纳的数量时(不确定是否为tableView内容Inset#import "ViewController.h"
#import "MyCell.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property UITableView *myTableView;
@property NSArray *array;
@end
@implementation ViewController
@synthesize myTableView, array;
- (void)viewDidLoad {
[super viewDidLoad];
myTableView = [[UITableView alloc] init];
myTableView.frame = CGRectMake(0, 100, 320, 300);
[self.view addSubview:myTableView];
[myTableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
myTableView.delegate = self;
myTableView.dataSource = self;
self.textField.delegate = self;
array = @[@"one", @"two"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.title.text = array[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array.count;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
myTableView.frame = CGRectMake(0, 100, 320, 100);
}
#导入“ViewController.h”
#导入“MyCell.h”
@界面视图控制器()
@属性UITableView*myTableView;
@属性NSArray*数组;
@结束
@实现视图控制器
@综合myTableView、array;
-(无效)viewDidLoad{
[超级视图下载];
myTableView=[[UITableView alloc]init];
myTableView.frame=CGRectMake(0,100320,300);
[self.view addSubview:myTableView];
[myTableView注册表类:[MyCell类]强制重用标识符:@“Cell”];
myTableView.delegate=self;
myTableView.dataSource=self;
self.textField.delegate=self;
数组=@[@“一”,@“二”];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
MyCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.title.text=数组[indexPath.row];
返回单元;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回array.count;
}
-(无效)textFieldDidBeginEditing:(UITextField*)textField{
myTableView.frame=CGRectMake(0,100320,100);
}
谢谢,@enharo2!并逐步增加单元格将tableView框架设置为原始?您只需截取textFieldShouldReturn方法并相应修改tableView.frame,这样它就可以调整为全屏。-(void)textFieldShouldReturn:(UITextField*)textField{myTableView.frame=self.view.frame}