問題還原:
以前在項目中經(jīng)常遇到在UITableView上添加UITextField,高度不超過屏幕還好恢暖,超過屏幕之后就會發(fā)現(xiàn)文本消失。問題如下:
未處理前.gif
因此舆床,就想到對輸入的數(shù)據(jù)進行保存嫁佳,然后再進行展示。部分代碼展示如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"id"];
if (!cell) {
cell = [[TableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"id"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//2.對輸入框tag綁定盛垦,并設(shè)置代理
cell.label.tag = indexPath.row;
cell.label.delegate = self;
cell.label.text = [_writeArray safe_objectAtIndex:indexPath.row];
return cell;
}
//3.對輸入的文本插入到數(shù)組中
- (void)textFieldDidEndEditing:(UITextField *)textField {
[_writeArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}
//4.獲取lastIndex
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
lastIndex = textField.tag;
return YES;
}
完成之后瓤漏,效果圖如下:
處理后.gif
如果這篇文章對你有所幫助颊埃,就給個贊??吧蝶俱。O(∩_∩)O哈哈~