我有一个非常复杂的UITableViewCell
。它包含UITableView里面,很多自定义视图等。并且取决于cellType我在自定义单元格对象的layoutSubviews
和updateConstraints
方法中以编程方式设置了一些约束。所以很明显我有一个不稳定的卷轴,当我至少有5个单元格因为我的单元格很多次布局。在cellForRowAtIndexPath
我为每个单元格调用
[cell setNeedsLayout];
[cell layoutIfNeeded];
如何优化UITableView
?设置所有值后,是否可以只布置一次?提前致谢。我真的被困了。
编辑:
我真的忘了展示代码。
- (ReservationCell *)approvedReservationCellInTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
reservation:(Reservation *)reservation
{
ReservationCell *cell = [tableView dequeueReusableCellWithIdentifier:kReservationCellXibAndReuseIdentifier forIndexPath:indexPath];
cell.isTouchingDisabledInTimeView = NO;
cell.indexPath = indexPath;
cell.tableView = self.tableView;
cell.bottomWrapper.indexPath = indexPath;
UIImage *placeholderImage = [UIImage imageNamed:@"userpic_dark_with_border"];
[cell.userpicImageView setImageWithURL:[NSURL URLWithString:reservation.guestInfo.userpic] placeholderImage:placeholderImage];
[cell setStatus:reservation.status];
cell.bottomWrapper.dataSource = self;
cell.bottomWrapper.delegate = self;
cell.nameLabel.text = reservation.guestInfo.fullName;
cell.reserverNameLabel.text = reservation.bookedBy.fullName;
cell.noteTextView.text = reservation.bookingNote.length > 0 ? reservation.bookingNote : @"";
BOOL hasAssignedStaff = reservation.staff.count > 0;
if (hasAssignedStaff) {
NSMutableArray *staffArray = [[NSMutableArray alloc] init];
for (SearchUserInfo *staffInfo in reservation.staff) {
[staffArray addObject:staffInfo];
}
[self.staffDictionaryForIndexPath setObject:staffArray forKey:indexPath];
} else {
[cell forceUpdateConstraints];
}
cell.delegate = self;
cell.dataSource = self;
cell.tableNameLabel.text = [self tableNameForReservation:reservation];
cell.minValueLabel.text = [self minValueForReservation:reservation];
cell.completionTimeLabel.text = reservation.estimatedArrivalTime.length > 1 ? [Tools timeWithAMPMFrom:reservation.estimatedArrivalTime] : @"--:--";
cell.guestsCountLabel.text = [NSString stringWithFormat:@"%d / %d", (int)reservation.arrivedGirls + (int)reservation.arrivedGuys, (int)reservation.totalGuests];
cell.bottomWrapper.shouldHideMenuButton = NO;
cell.cellType = ReservationCellTypeApproved;
[cell setNeedsLayout];
[cell layoutIfNeeded];
return cell;
}
- (void)setDataSource:(id<ReservationCellDataSource>)dataSource
{
self->_dataSource = dataSource;
[self.staffTableView reloadData];
[self.collectionView reloadData];
[self updateTagsWrapperConstraint];
[self updateCompletedViewTopGraylineState];
}
- (void)updateCompletedViewTopGraylineState
{
if ([self.dataSource respondsToSelector:@selector(staffNamesArrayInReservationCell:)]) {
NSArray *names = [self.dataSource staffNamesArrayInReservationCell:self];
NSArray *tagsArray = [self.dataSource tagsArrayInReservationCell:self];
BOOL isCorrectStatus = self.reservationStatus == ReservationStatusCompleted
|| self.reservationStatus == ReservationStatusConfirmedComplete;
if (names.count == 0 && tagsArray.count == 0 &&
self.bottomWrapper.wrapperHeight == 0 && isCorrectStatus && self.noteTextView.text.length == 0) {
self.completedView.topGrayLine.hidden = YES;
self.completedViewTopConstraint.constant = 0.f;
} else {
self.completedView.topGrayLine.hidden = NO;
if (self.bottomWrapper.wrapperHeight == 0) {
self.completedViewTopConstraint.constant = 9.f;
} else {
self.completedViewTopConstraint.constant = 0.f;
}
self.completedViewTopConstraint.constant = 0.f;
}
}
}
- (void)configureConstraints
{
if (self.cellType == ReservationCellTypeUndefined) {
return;
}
CGFloat rightButtonsWrapperWidth = 0;
CGFloat rightButtonsWrapperHeight = 0;
CGFloat rithWrapperApproveWidth = 0;
CGFloat rithWrapperApproveHeight = 0;
CGFloat arrivedButtonWidth = 0;
CGFloat arrivedButtonHeight = 0;
if (self.cellType == ReservationCellTypeCustomersProfile) {
if (self.reservationStatus == ReservationStatusPending) {
//self.statusLabel.text = @"";
}
if (self.reservationStatus == ReservationStatusArrived) {
}
}
if (self.cellType == ReservationCellTypeSeating) {
if (self.reservationStatus == ReservationStatusArrived) {
rightButtonsWrapperWidth = 100.f;
rightButtonsWrapperHeight = 100.f;
} else {
rightButtonsWrapperWidth = 0;
rightButtonsWrapperHeight = 0;
}
}
if (self.cellType == ReservationCellTypePickReservation) {
arrivedButtonWidth = 80.f;
arrivedButtonHeight = 25.f;
}
if (self.cellType == ReservationCellTypePending) {
if (self.reservationStatus == ReservationStatusPending) {
rithWrapperApproveWidth = 45.f;
rithWrapperApproveHeight = 115.f;
}
}
if (self.cellType == ReservationCellTypeGuestlist) {
rightButtonsWrapperWidth = 100.f;
rightButtonsWrapperHeight = 100.f;
}
[self.rightButtonsWrapper mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(rightButtonsWrapperWidth);
make.height.mas_equalTo(rightButtonsWrapperHeight);
}];
[self.rightWrapperApprove mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(rithWrapperApproveWidth);
make.height.mas_equalTo(rithWrapperApproveHeight);
}];
if (self.cellType != ReservationCellTypeCustomersProfile) {
if (self.reservationStatus == ReservationStatusCompleted || self.reservationStatus == ReservationStatusConfirmedComplete) {
self.rightButtonsWrapperTopConstraint.constant = 0.f;
self.rightWrapperApproveTopConstraint.constant = 0.f;
CGFloat height = 0;
if (self.cellType == ReservationCellTypeEndOfDay) {
CGFloat labelHeight = [self labelHeight:self.completedView.completionCommentLabel];
height = 202.f + labelHeight;
}
[self.completedView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height);
}];
} else {
[self.completedView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0.f);
}];
}
}
BOOL isTypeForArrivedButton = self.cellType == ReservationCellTypeSeating || self.cellType == ReservationCellTypeEndOfDay;
if (self.reservationStatus == ReservationStatusApproved && isTypeForArrivedButton) {
arrivedButtonWidth = 80.f;
arrivedButtonHeight = 25.f;
}
[self.arrivedButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(arrivedButtonHeight);
make.width.mas_equalTo(arrivedButtonWidth);
}];
// if (self.cellType == ReservationCellTypePending) {
// [self.rightWrapperApprove mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.bottom.mas_greaterThanOrEqualTo(self.bottomWrapper.mas_top).with.offset(10);
// }];
// }
if (self.cellType == ReservationCellTypeSeating) {
if (self.reservationStatus == ReservationStatusArrived) {
[self addRightButtonsWrapperExcludedPathWidth:rightButtonsWrapperWidth height:rightButtonsWrapperHeight];
}
}
if (self.cellType == ReservationCellTypeGuestlist) {
[self addRightButtonsWrapperExcludedPathWidth:rightButtonsWrapperWidth height:rightButtonsWrapperHeight];
}
if (self.cellType == ReservationCellTypePending) {
if (self.reservationStatus == ReservationStatusPending) {
[self addRightWrapperApproveExcludedPathWidth:rithWrapperApproveWidth height:rithWrapperApproveHeight];
}
}
[self updateTopLabelsViewHeightConstraint];
}