美文网首页
iOS 滑动UITableView,UITextField文字消

iOS 滑动UITableView,UITextField文字消

作者: XieHenry | 来源:发表于2017-07-04 16:52 被阅读170次

问题还原:

以前在项目中经常遇到在UITableView上添加UITextField,高度不超过屏幕还好,超过屏幕之后就会发现文本消失。问题如下:

未处理前.gif

因此,就想到对输入的数据进行保存,然后再进行展示。部分代码展示如下:

- (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绑定,并设置代理
    cell.label.tag = indexPath.row;
    cell.label.delegate = self;
    cell.label.text = [_writeArray safe_objectAtIndex:indexPath.row];

    return cell;
}
//3.对输入的文本插入到数组中
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [_writeArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}

//4.获取lastIndex
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    lastIndex = textField.tag;
    return YES;
}

完成之后,效果图如下:

处理后.gif

demo链接

如果这篇文章对你有所帮助,就给个赞👍吧。O(∩_∩)O哈哈~

相关文章

网友评论

      本文标题:iOS 滑动UITableView,UITextField文字消

      本文链接:https://www.haomeiwen.com/subject/kwrxhxtx.html