UITableView 滞后于第一次显示包含 UITextView 的单元格 [英] UITableView is lagging a cell containing a UITextView is displayed the first time

查看:26
本文介绍了UITableView 滞后于第一次显示包含 UITextView 的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 UITableView 的问题,该问题在特定单元格移动到超级视图时滚动时滞后.

I'm running into issues with a UITableView that lags while scrolling when specific cells will move to superview.

我已经编写了自己的 IPFormKit,以便轻松创建具有不同类型 inputViews 的漂亮输入表单,而无需为每个表单字段/单元格手动重新编码所有内容.

I've written my own IPFormKit for an easy way to create beautiful input forms with different kind of inputViews without having to re-code everything manually for each form field / cell.

我有一个 UITableViewController 来初始化我的 IPFormKit 及其字段.- (UITableViewCell *) cellForRowAtIndexPath:(NSIndexPath)indexPath; 加载出列的自定义单元格(称为 IPFormTableViewCell)并将 IPFormField 分配给每个单元格.

I've got a UITableViewController that initializes my IPFormKit and its fields. The - (UITableViewCell *) cellForRowAtIndexPath:(NSIndexPath)indexPath; loads the dequeued custom cells (called IPFormTableViewCell) and assigns the IPFormField to each cell.

自定义 UITableViewCell (IPFormTableViewCell) 在初始化时使用 CGRectZero 创建所有(可能)必需的 inputViews(UITextField、UITextView、CustomUILabel).

The custom UITableViewCell (IPFormTableViewCell) creates all (possibly) required inputViews (UITextField, UITextView, CustomUILabel) with a CGRectZero on initialization.

匹配的 inputView 取决于 IPFormField 的类型(已经作为单元的 iVar 初始化)被调整大小并作为子视图添加到 cell.contentView.

The matching inputView depending on the IPFormField's type (that was already inited as an iVar of the cell) is resized and added as a subview to the cell.contentView within.

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath)indexPath

对于 UITextField 和 CustomUILabel 这可以完美地工作,但是 当 inputView 是 UITextView 时,UITableView 的滚动在第一次显示此单元格时会(略微)明显滞后.

For UITextField and CustomUILabel this works flawlessly, but when the inputView is a UITextView, the scrolling of the UITableView lags (slightly) noticable when this cell will be displayed for the first time.

当单元格在滚动一点后再次显示时(即使单元格被重用,因此 UITextView 被删除并读取),没有延迟并且这些单元格的滚动非常平滑.

When the cell will be displayed again later after scrolling a bit (even if the cell was reused and thus the UITextView removed and readded), there is no lag and scrolling is super smooth for those cells.

我想不出这种滞后的原因可能是什么.任何想法表示赞赏.

I'm running out of ideas what the reason for this lag could be. Any idea is appreciated.

PS:延迟在 iPhone 4 和 iPhone 4S 上都很明显,并且持续时间几乎完全相同(因此应该与 CPU 无关)

PS: The lag is noticable on both, iPhone 4 and iPhone 4S and is of almost exactly the same duration (so it should not be CPU related)

UITableViewController.m:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"IPFormFieldCell";

    // Get Form Field for indexPath
    IPFormField *formField = [self.form fieldAtIndexPath:indexPath];

    IPTableViewCell *cell = (IPTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[IPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundView = nil;
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
        cell.selectedBackgroundView = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    [cell assignFormField:formField];

    return cell;
}  

IPFormTableViewCell.m:

- (void)assignFormField:(IPFormField *)field:

- (void) assignFormField:(IPFormField *)field {
    if (formField != nil) {
        formField.inputView = nil; // unlink old field
    }

    self.formField = field;

    // Change Field Label
    [fieldLabel setText:[field label]];

    // Add an Input View to the Field
    UIView *labelView = nil;
    UIView *inputView = nil;

    switch (formField.type) {
        case IPFormFieldTypeTextField:
        {
            labelView = fieldLabel;

            UITextField *textField = inputTextField;
            textField.delegate = (IPFormTextField *)formField;
            textField.inputAccessoryView = [formField.form inputAccessoryView];
            textField.placeholder = [self.formField stringFromValue:self.formField.defaultValue];
            textField.keyboardType = [(IPFormTextField *)formField keyboardType];

            if (self.formField.value == nil || [[self.formField stringFromValue:self.formField.value] isEqualToString:[self.formField stringFromValue:self.formField.defaultValue]]) {
                textField.clearsOnBeginEditing = YES;
            } else {
                textField.text = [self.formField stringFromValue:self.formField.value];
                textField.clearsOnBeginEditing = NO;
            }

            inputView = textField;
            break;
        }

        case IPFormFieldTypeTextArea:
        {            
            UITextView *textView = inputTextView;
            textView.delegate = (IPFormTextArea *)formField;
            textView.inputAccessoryView = [formField.form inputAccessoryView];

            if (self.formField.value == nil || ![[self.formField stringFromValue:self.formField.value] length] > 0) {
                textView.text = [self.formField stringFromValue:self.formField.defaultValue];
            } else {
                textView.text = [self.formField stringFromValue:self.formField.value];
            }

            inputView = textView;
            break;
        }

        default:
            break;
    }

    self.leftItem = labelView;
    self.rightItem = inputView;

    if (leftItem != nil) {
        [self.contentView addSubview:leftItem];
    }

    if (rightItem != nil) {
        [self.contentView addSubview:rightItem];
    }

    formField.inputView = rightItem;
}

推荐答案

显然,我的数据源的 cellForRowAtIndexPath: 使用了一个字段的属性,该属性被设置为 @property (nonatomic,copy) 而不是 @property (nonatomic, readonly).

Apparently, cellForRowAtIndexPath: of my dataSource made use of a field's property, that was set as @property (nonatomic, copy) instead of @property (nonatomic, readonly).

现在我已经修复了它,滚动不再滞后.

Now that I've fixed it, the scrolling isn't lagging anymore.

这篇关于UITableView 滞后于第一次显示包含 UITextView 的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆