标签: iOS UITextField cell
有这样一种需求,需要创建一些信息上传到服务器,那么在界面上,就需要进行文本输入,如果一个或者两个文本框,那么就好实现,不需要用UITableViewCell了,但如果是多个文本输入呢,就不得不使用UITableViewCell,那么随之而来的是UITextField文本随着Cell复用带来了问题,即当滑动操作时,文本框的内容会发生错乱等,如何解决呢?
我的解决方案如下:
- 创建一个模型,用这个模型声明一些属性,用来保存cell中UITextField的值,并且利用KVC提供字典转模型方法
- 创建UITableViewController为业务VC,自定义cell,给cell传入模型,用来给UITextfield赋值
- cell.h文件中定义代理,用来回调UITextField的值
- 在VC中创建并初始化模型属性,在代理实现中给模型赋值
具体代码如下:
model
@interface TCTextModel : NSObject
@property (nonatomic, strong) NSString *text0;
@property (nonatomic, strong) NSString *text1;
@property (nonatomic, strong) NSString *text2;
@property (nonatomic, strong) NSString *text3;
@property (nonatomic, strong) NSString *text4;
@property (nonatomic, strong) NSString *text5;
@property (nonatomic, strong) NSString *text6;
@property (nonatomic, strong) NSString *text7;
@property (nonatomic, strong) NSString *text8;
@property (nonatomic, strong) NSString *text9;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
@implementation TCTextModel
- (instancetype)initWithDict:(NSDictionary *)dict {
self = [super init];
if (self) {
_text0 = dict[@"text0"];
_text1 = dict[@"text1"];
_text2 = dict[@"text2"];
_text3 = dict[@"text3"];
_text4 = dict[@"text4"];
_text5 = dict[@"text5"];
_text6 = dict[@"text6"];
_text7 = dict[@"text7"];
_text8 = dict[@"text8"];
_text9 = dict[@"text9"];
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}
@end
view
@class TCTextModel;
@protocol TCTextFiledCellDelegate;
@interface TCTextFiledCell : UITableViewCell
@property (nonatomic, weak) id<TCTextFiledCellDelegate> delegate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) TCTextModel *model;
@end
@protocol TCTextFiledCellDelegate <NSObject>
@optional
- (void)textFiledCell:(TCTextFiledCell *)cell text:(NSString *)text index:(NSInteger)index;
@end
@interface TCTextFiledCell () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@end
@implementation TCTextFiledCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self setupUI];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
#pragma mark - UI
- (void)setupUI {
self.textField.delegate = self;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([self.delegate respondsToSelector:@selector(textFiledCell:text:index:)]) {
[self.delegate textFiledCell:self text:textField.text index:self.index];
}
}
#pragma mark - setter
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = _title;
}
- (void)setIndex:(NSInteger)index {
_index = index;
self.tipLabel.text = [NSString stringWithFormat:@"第%@个", @(_index)];
switch (_index) {
case 0: {
self.textField.text = self.model.text0;
break;
}
case 1: {
self.textField.text = self.model.text1;
break;
}
case 2: {
self.textField.text = self.model.text2;
break;
}
case 3: {
self.textField.text = self.model.text3;
break;
}
case 4: {
self.textField.text = self.model.text4;
break;
}
case 5: {
self.textField.text = self.model.text5;
break;
}
case 6: {
self.textField.text = self.model.text6;
break;
}
case 7: {
self.textField.text = self.model.text7;
break;
}
case 8: {
self.textField.text = self.model.text8;
break;
}
case 9: {
self.textField.text = self.model.text9;
break;
}
default:
break;
}
}
@end
controller
static NSString * const kTCTextFiledCellIdentifier = @"TCTextFiledCell";
@interface TCTextFieldListController () <TCTextFiledCellDelegate>
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) TCTextModel *textModel;
@end
@implementation TCTextFieldListController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"textField列表";
self.view.backgroundColor = [UIColor whiteColor];
[self setupUI];
[self setupData];
}
#pragma mark - UI
- (void)setupUI {
self.tableView.rowHeight = 100;
self.tableView.tableFooterView = [UIView new];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UINib *nib = [UINib nibWithNibName:@"TCTextFiledCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:kTCTextFiledCellIdentifier];
}
- (void)setupData {
NSDictionary *dict = @{@"text0": @"",
@"text1": @"",
@"text2": @"",
@"text3": @"",
@"text4": @"",
@"text5": @"",
@"text6": @"",
@"text7": @"",
@"text8": @"",
@"text9": @""};
self.textModel = [[TCTextModel alloc] initWithDict:dict];
self.dataArr = @[@"目录0", @"目录1", @"目录2", @"目录3", @"目录4", @"目录5", @"目录6", @"目录7", @"目录8", @"目录9"];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TCTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:kTCTextFiledCellIdentifier forIndexPath:indexPath];
cell.title = self.dataArr[indexPath.row];
cell.model = self.textModel;
cell.delegate = self;
cell.index = indexPath.row;
return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self.view endEditing:YES];
}
#pragma mark - TCTextFiledCellDelegate
- (void)textFiledCell:(TCTextFiledCell *)cell text:(NSString *)text index:(NSInteger)index {
switch (index) {
case 0: {
self.textModel.text0 = text;
break;
}
case 1: {
self.textModel.text1 = text;
break;
}
case 2: {
self.textModel.text2 = text;
break;
}
case 3: {
self.textModel.text3 = text;
break;
}
case 4: {
self.textModel.text4 = text;
break;
}
case 5: {
self.textModel.text5 = text;
break;
}
case 6: {
self.textModel.text6 = text;
break;
}
case 7: {
self.textModel.text7 = text;
break;
}
case 8: {
self.textModel.text8 = text;
break;
}
case 9: {
self.textModel.text9 = text;
break;
}
default:
break;
}
}
@end
网友评论