Skip to content

Commit cc9ff24

Browse files
author
wangyutao
committed
fix accessory width for plus and section index
1 parent 912b3ce commit cc9ff24

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

Classes/UITableView+FDTemplateLayoutCell.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ - (CGFloat)fd_systemFittingHeightForConfiguratedCell:(UITableViewCell *)cell {
3232
cellBounds.size.width = contentViewWidth;
3333
cell.bounds = cellBounds;
3434

35-
CGFloat accessroyWidth = 0;
35+
CGFloat rightSystemViewsWidth = 0.0;
36+
for (UIView *view in self.subviews) {
37+
if ([view isKindOfClass:NSClassFromString(@"UITableViewIndex")]) {
38+
rightSystemViewsWidth = CGRectGetWidth(view.frame);
39+
break;
40+
}
41+
}
42+
3643
// If a cell has accessory view or system accessory type, its content view's width is smaller
3744
// than cell's by some fixed values.
3845
if (cell.accessoryView) {
39-
accessroyWidth = 16 + CGRectGetWidth(cell.accessoryView.frame);
46+
rightSystemViewsWidth += 16 + CGRectGetWidth(cell.accessoryView.frame);
4047
} else {
4148
static const CGFloat systemAccessoryWidths[] = {
4249
[UITableViewCellAccessoryNone] = 0,
@@ -45,9 +52,14 @@ - (CGFloat)fd_systemFittingHeightForConfiguratedCell:(UITableViewCell *)cell {
4552
[UITableViewCellAccessoryCheckmark] = 40,
4653
[UITableViewCellAccessoryDetailButton] = 48
4754
};
48-
accessroyWidth = systemAccessoryWidths[cell.accessoryType];
55+
rightSystemViewsWidth += systemAccessoryWidths[cell.accessoryType];
4956
}
50-
contentViewWidth -= accessroyWidth;
57+
58+
if ([UIScreen mainScreen].scale >= 3.0) {
59+
rightSystemViewsWidth += 4;
60+
}
61+
62+
contentViewWidth -= rightSystemViewsWidth;
5163

5264

5365
// If not using auto layout, you have to override "-sizeThatFits:" to provide a fitting size by yourself.
@@ -79,7 +91,7 @@ - (CGFloat)fd_systemFittingHeightForConfiguratedCell:(UITableViewCell *)cell {
7991

8092
// Build edge constraints
8193
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
82-
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeRight multiplier:1.0 constant:accessroyWidth];
94+
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeRight multiplier:1.0 constant:-rightSystemViewsWidth];
8395
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
8496
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
8597
edgeConstraints = @[leftConstraint, rightConstraint, topConstraint, bottomConstraint];

Demo/Demo/FDFeedViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,8 @@ - (void)deleteSection {
188188
}
189189
}
190190

191+
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
192+
return @[@"A"];
193+
}
194+
191195
@end

0 commit comments

Comments
 (0)