我在向上滚动时在UITableViewController上处理一个奇怪的问题。 (见下面的视频)
UITableViewCell(一个自定义的)是高度变量,使用Autolayout和新的iOS8 estimateRowHeight和rowHeight在viewDidLoad中设置为UITableViewAutomaticDimension。该表有1000行,并使用NSFetchedResultsController 我尝试过Apple的示例,来自StackOverflow,可以很好地处理50或100个单元格,但不多了。 我也试过故事板中的约束和代码中的约束 欢迎任何建议。
视频: http://youtu.be/0Jna4rXIRAM
UITableViewController代码:
-(void)viewWillAppear:(BOOL)animated {
self.myTableView.contentInset = UIEdgeInsetsMake(64, 0, 70, 0);
self.myTableView.rowHeight = UITableViewAutomaticDimension;
self.myTableView.estimatedRowHeight = 60.0f;
[self.myTableView registerClass:[ShotTableViewCell class] forCellReuseIdentifier:CellIdentifier];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ShotTableViewCell *cell = (ShotTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell atIndexPAth:indexPath];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
-(void)configureCell:(ShotTableViewCell *)cell atIndexPAth:(NSIndexPath *)indexPath {
Shot *shot = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell configureBasicCellWithShot:shot andRow:indexPath.row];
[cell addTarget:self action:@selector(goProfile:)];
}
-(NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
//GET THE DATA
self.fetchedResultsController = [[ShotManager singleton] getShotsForTimeLine];
self.fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.myTableView beginUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.myTableView;
switch(type) {
case NSFetchedResultsChangeInsert:{
self.tableTypeOfChange = k_Insert;
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeDelete:{
self.tableTypeOfChange = k_Delete;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeUpdate:{
self.tableTypeOfChange = k_Update;
break;
}
case NSFetchedResultsChangeMove:{
self.tableTypeOfChange = k_Move;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.myTableView endUpdates];
}
ShotTableViewCell代码:
-(void)configureBasicCellWithShot:(Shot *)shot andRow:(NSInteger)row {
self.txvText.attributedText = [TimeLineUtilities filterLinkWithContent:shot.comment];
self.lblName.text = shot.user.userName;
self.imgPhoto = [DownloadImage downloadImageForTimeLineWithUrl:[NSURL URLWithString:shot.user.photo] andUIimageView:self.imgPhoto andText:[shot.user.name substringToIndex:1]];
self.lblDate.text = [TimeLineUtilities getDateShot:shot.csys_birth];
self.btnPhoto.tag = row;
}
-(void)layoutSubviews {
[super layoutSubviews];
// Make sure the contentView does a layout pass here so that its subviews have their frames set, which we
// need to use to set the preferredMaxLayoutWidth below.
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
// Set the preferredMaxLayoutWidth of the mutli-line bodyLabel based on the evaluated width of the label's frame,
// as this will allow the text to wrap correctly, and as a result allow the label to take on the correct height.
self.txvText.preferredMaxLayoutWidth = CGRectGetWidth(self.txvText.frame)
}