UITableView内输入框(UITextView)换行

业务描述:

UITableView内存在一个Cell,该Cell内有一输入框可以输入文字,超出输入框宽度则换行展示,即该Cell高度要增加
如图:
未换行情况

换行情况

解决方法:

思路:
1:取到最大输入框宽度
2:计算当前文字宽度与最大输入框宽度用以判断是否应该换行(Cell高度增加)
3:判断是否换行后更新UITableView即可。

注意点:

换行

  • UITextFiled是不能换行的,只能使用UITextView进行文本输入框功能使用,所以如果项目里是UITextField必须要替换成UITextView;

回调事件

UITextFiled继承自UIControl,可以使用addTarget或代理方法,而UITextView继承自UIScrollView, 只有代理方法。那么解决方法就是在Cell内注册UITextView的代理,.h文件里面(swift的public属性)声明对应的block回调,例如:
cell的.h文件:

/** 输入框内容已经改变的Block */
@property (nonatomic, copy) void(^textViewDidChangeBlock)(UITextView *textView);

/** 输入框已经开始编辑的Block */
@property (nonatomic, copy) void(^textViewDidBeginEditingBlock)(UITextView *textView);

/** 输入框是否能被编辑的Block */
@property (nonatomic, copy) BOOL(^textViewShouldBeginEditingBlock)(UITextView *textView);

cell的.m文件:

#pragma mark - Delegate
- (void)textViewDidChange:(UITextView *)textView {
    if (self.textViewDidChangeBlock){
        self.textViewDidChangeBlock(textView);
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if (self.textViewDidBeginEditingBlock){
        self.textViewDidBeginEditingBlock(textView);
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (self.textViewShouldBeginEditingBlock){
        return self.textViewShouldBeginEditingBlock(textView);
    }
    
    return YES;
}

这样,在cellForRowAtIndexPath方法里面就能拿到Cell内的UITextView回调事件进行业务判断了。当然如果业务是以其他形式编写的可以自由发挥,不一定放在UIViewController里面或者cellForRowAtIndexPath方法内处理。

UITextView的文字Inset

  • UITexView的文字与其本身不是贴合的,是有间隙的。UITextView内存在textContainer用以管理文字,可以通过
_textView.textContainerInset = UIEdgeInsetsZero;
_textView.textContainer.lineFragmentPadding = 0;

这两个属性来取消间隙。

UITextView的placeholder属性

  • 这个问题解决途径多样化,可以直接设置UILabel,每次UITextView文字改变时会判断text的长度,大于0就隐藏UILabel。

UITextView的文字竖直方向上居中

当然,不是所有的人的业务都是相同的,设置完成后可能发现并不符合自己的业务需求,发现文字不是竖直方向的居中。这时候可能会想到设置UITextView的contentOffset。然而直接设置是没有效果的,contentOffset必须要在主线程里面强制刷新才有效(不清楚具体原因),如下:

    [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
        [self.textView setContentOffset: CGPointMake(0, -4)];
    }];

那这个代码在什么地方调用呢?建议对UITextView设置一个kvo,监听contentSize属性,在- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {里面更新。因为如果在Cell的init方法里面设置UITextView的contentOffset,是只对那一时刻的UITextView生效,一旦有文字内容变换,其contentOffset又会重新计算,导致前面失效。而在KVO里面只要文字内容改变就会触发KVO回调,最后更新contentOffset会生效。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {

    [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
        [self.textView setContentOffset: CGPointMake(0, -4)];
    }];
}

刷新UITableView导致Cell内的UITextView焦点丢失

  • 这个情况应该是使用了 [tableView reloadData]或者指定section等方法导致的,正确的是使用:
[self.tableView performBatchUpdates:^{
    
} completion:nil];![请添加图片描述](https://img-blog.csdnimg.cn/154f5437d35b4e7fa89856f6d0ffc6e7.jpeg)

去刷新就 ok。定义几个控制Cell高度的属性以及是否应该换行用以在UITableView的代理方法中去使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值