Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果单元格数量少于一个屏幕,则滚动UITableView_Ios_Objective C_Uitableview_Scroll - Fatal编程技术网

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那么,如何仅使用几个单元格滚动此tableView?

一个选项是在编辑开始时调整tableView框架的大小。见下面的例子。(表视图是通过编程实例化的,包括单元格寄存器。有了调整大小的表视图,卷轴应该可以工作。我希望它能帮助…e。)

#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}