UITableView中cell里的UITextField不被弹出键盘挡住

简介:

UITableView中cell里的UITextField不被弹出键盘挡住

 

 本人视频教程系类   iOS中CALayer的使用

 

效果如下:

源码:

EditCell.h 与 EditCell.m

//
//  EditCell.h
//  Cell
//
//  Created by YouXianMing on 14/12/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface EditCell : UITableViewCell

@property (nonatomic, strong) UITextField *field;

@end


//
//  EditCell.m
//  Cell
//
//  Created by YouXianMing on 14/12/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "EditCell.h"

@implementation EditCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
        line.backgroundColor = [UIColor colorWithRed:0.886 green:0.918 blue:0.933 alpha:1];
        [self addSubview:line];
        
        _field               = [[UITextField alloc] initWithFrame:CGRectMake(20, 2, 280, 42)];
        _field.textColor     = [UIColor grayColor];
        _field.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18];
        [self addSubview:_field];
    }
    
    return self;
}

@end

ViewController.m
//
//  ViewController.m
//  Cell
//
//  Created by YouXianMing on 14/12/18.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "EditCell.h"

static NSInteger number = 8;

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UITableView    *tableView;
@property (nonatomic, weak)   UITextField    *tmpTextField; // 获取当前编辑的TextField
@property (nonatomic, strong) NSMutableArray *strsArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 数据源(初始化)
    _strsArray = [NSMutableArray array];
    for (int i = 0; i < number; i++) {
        [_strsArray addObject:@""];
    }
    
    // 初始化tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                              style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor colorWithRed:0.949 green:0.957 blue:0.961 alpha:1];
    [self.view addSubview:_tableView];
    
    _tableView.delegate       = self;
    _tableView.dataSource     = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[EditCell class] forCellReuseIdentifier:@"YouXianMing"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return number;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditCell *cell      = [tableView dequeueReusableCellWithIdentifier:@"YouXianMing"];
    cell.field.delegate = self;
    cell.field.text     = _strsArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - UITextField代理
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    // 获取到临时的textField并存储起来
    self.tmpTextField = textField;
    
    // 获取到父类cell
    EditCell *cell    = (EditCell *)[self.tmpTextField superview];
    
    // 获取到indexPath
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
    
    // 执行动画(移动到输入的位置)
    [self.tableView setContentOffset:CGPointMake(0, (path.row)*44) animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    // 获取到临时的textField并存储起来
    self.tmpTextField = textField;
    
    // 获取到父类cell
    EditCell *cell    = (EditCell *)[self.tmpTextField superview];
    
    // 获取到indexPath
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
    
    // 存储到数据源中
    [_strsArray replaceObjectAtIndex:path.row
                          withObject:(textField.text == nil ? @"" : textField.text)];
    
    // 打印信息
    NSLog(@"%@", _strsArray);
}
- (BOOL)textFieldShouldReturn:(UITextField *)sender {
    
    // 执行动画(恢复到原始位置)
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    // 交出第一响应者
    [sender resignFirstResponder];
    
    return YES;
}

@end

以下是需要注意的地方:

通过父视图获取到了cell,然后根据cell获取indexPath值,然后可以做事情了

核心代码是根据第几个cell来执行位移的动画,这个值是可以调整的。

 

目录
相关文章
|
iOS开发
iOS开发之有间距的UITableViewCell
UITableView是最常用的一个iOS控件,现要做一个如下图的UITableView,其中白色部分就是cell,可是默认的UITableView中cell之间是没有间隔的,那么办呢?网上有2种做法,我这里顺带提一下吧 效果图.png 1、方式一 通过设置cell的contentView来间接实现,在cell的contentView的顶部或者底部留下一定的间距,这样就会有cell间就有间距的效果。
1278 0
|
9月前
CollectionView 单个选项卡的滑动
最近在做一个旅行类的项目,里面哟孤儿横向滑动的选项卡功能,乍一看设计图,感觉很简单。横向滑动,CollectionView的flowLayout有这个设置属性,分分钟搞定。后来需求要每次滑动一个选项卡。这就让我有点棘手了,因为心里知道这个应该是要自己去计算偏移量的问题了
UILabel的文字在左上角显示
UILabel的文字在左上角显示
242 0
UILabel的文字在左上角显示
|
iOS开发
修改UISearchBar背景颜色
修改UISearchBar背景颜色
135 0
TableView自动滚动到底部
TableView自动滚动到底部
174 0
|
iOS开发 开发者
iOS开发中UITableViewCell点击时子视图背景透明的解决方法
iOS开发中UITableViewCell点击时子视图背景透明的解决方法
176 0
iOS开发中UITableViewCell点击时子视图背景透明的解决方法