将多个 UITextView 设置为可编辑会导致光标在它们之间闪烁

发布于 2024-09-13 17:26:27 字数 990 浏览 2 评论 0原文

当用户点击 UITableView 的“编辑”按钮时,我尝试将一组 UITableViewCell 中的 UITextView 设置为可编辑。每个 UITableViewCell 都有一个 UITextField (cell.blurb)。此代码会将每个 UITextView 设置为可编辑,但是,光标在每个 UITextView 之间快速交替。我很确定这是一个响应者链问题,(也许他们都成为了第一响应者?)但是我似乎无法补救。我尝试让每个 UITextView resignFirstResponder (除了列表中的第一个),但它什么也没做。在表格单元格 xib 中,它们是不可编辑的。

//set all text areas to editable and opaque
int numSections = [_tableView numberOfSections];
for (int s = 0; s < numSections; s++) {
    int numRows = [_tableView numberOfRowsInSection: s];
    for (int r = 0; r < numRows; r++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:r inSection:s];
        CustomTableViewCell *cell = (CustomTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
        [cell blurb].editable = YES;
        [[cell blurb] resignFirstResponder];

        //set the first row's UITableView to the first responder
        if (s == 0 && r == 0)
            [[cell blurb] becomeFirstResponder];
    }
}

I'm attempting to set the UITextViews in a set of UITableViewCell's to editable when the user taps the "edit" button for the UITableView. Each UITableViewCell has a UITextField (cell.blurb). This code will set each UITextView to editable, however, the cursor alternates very rapidly between each one. I'm pretty sure it's a responder chain issue, (they're all becoming first responders maybe?) however I can't seem to remedy it. I've tried having each UITextView resignFirstResponder (save for the first one in the list), but it does nothing. In the table cell xib they're uneditable.

//set all text areas to editable and opaque
int numSections = [_tableView numberOfSections];
for (int s = 0; s < numSections; s++) {
    int numRows = [_tableView numberOfRowsInSection: s];
    for (int r = 0; r < numRows; r++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:r inSection:s];
        CustomTableViewCell *cell = (CustomTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
        [cell blurb].editable = YES;
        [[cell blurb] resignFirstResponder];

        //set the first row's UITableView to the first responder
        if (s == 0 && r == 0)
            [[cell blurb] becomeFirstResponder];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-09-20 17:26:27

我设法解决的唯一解决方案是创建两个 UITextView,一个可编辑,一个不具有相同的框架和属性,并根据可编辑状态管理每个 UITextView 的不透明度。很蹩脚不是吗?

另一件事,我必须在我的委托(恰好是 UITableViewCell 子类)中实现这一点:

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

否则,由于未知原因,UITableView 动画的结束(当它退出编辑模式时)再次触发其中一个 UITextView 上的变为FirstResponder。

这是 devforums 上的一个帖子 https://devforums.apple.com/message/290194

The only solution I managed to make work is create two UITextViews, one editable, one not with the same frame and properties, and manage opacity of each according to editable state. Quite lame isn't it ?

Another thing, I had to implement this in my delegate (which happens to be the UITableViewCell subclass):

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

otherwise, for an unknown reason, the end of the UITableView animation (when it quits editing mode) triggers again becomeFirstResponder on one of the UITextView.

Here is a thread on devforums https://devforums.apple.com/message/290194

情魔剑神 2024-09-20 17:26:27
if (s == 0 && r == 0) {
  [[cell blurb] becomeFirstResponder];
} else if ([[cell blurb] isFirstResponder]) {
  [[cell blurb] resignFirstResponder];
}
if (s == 0 && r == 0) {
  [[cell blurb] becomeFirstResponder];
} else if ([[cell blurb] isFirstResponder]) {
  [[cell blurb] resignFirstResponder];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文