Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

iphone - dequeued UITableViewCell has incorrect layout until scroll (using autolayout)

I have a custom UITableViewCell subclass which has had autolayout constraints applied to it in Interface Builder. The cell contains multiple views, including a UITextField.

Relevantly, the size of the UITextField is constrained such that there is default horizontal spacing between it and the next view.

The cell is instantiated like follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProgressCell";
    ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                     forIndexPath:indexPath]

    cell.textField.text = @"Some string that is different for each cell";

    return cell;
}

When the cell first appears, the UITextField overruns the correct frame, and appears behind the UIView to its right. However, when I scroll the cell off screen, pause, and then scroll back, the text is truncated correctly.

An example is shown below (at the second edit).

I have tried calling [cell setNeedsLayout] and [cell setNeedsDisplay] for the cell in cellForRowAtIndexPath, as well as performing them after a delay. Neither is effective.

What is scrolling off screen doing that is causing the cell to appear correctly, and how can I either replicate this or fix the underlying issue?

EDIT:

Calling

[self.tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

in order to reload the cell, appears to cause the layout to appear correctly first time.

However, it now breaks (occasionally) on scrolling (ie, when scrolling back up, the layout constraints are now not applied correctly).

Calling [cell setNeedsLayout] in cellForRowAtIndexPath appears not to fix this issue.

EDIT2:

The top cell, as shown here, appeared correctly (as the bottom cell does) until I scrolled down the screen. It since disappeared.

This reflects the problem as of the first edit - it's the second rendering that is the problem (makes me think that it might have something to do with reusing the cell?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you name a property on a UITableViewCell subclass textLabel or defaultTextLabel, then IB will ignore the constraints you have specified and override them with default ones, with no warnings issued.

This is the case even on cells designed in IB with the Custom style, which have no visible textLabel or detailTextLabel properties.

This also happen if add a property of type UIImageView property on a UITableViewCell subclass and name it imageView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...