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

时间:2010-08-06 13:30:04

标签: iphone cocoa-touch uitextview

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

//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];
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

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

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

这是devforums https://devforums.apple.com/message/290194

上的一个主题

答案 1 :(得分:0)

if (s == 0 && r == 0) {
  [[cell blurb] becomeFirstResponder];
} else if ([[cell blurb] isFirstResponder]) {
  [[cell blurb] resignFirstResponder];
}
相关问题