Commit de350e48 authored by stkhapugin@chromium.org's avatar stkhapugin@chromium.org Committed by Commit Bot

Create a self-sizing table view.

Creates a new class, SelfSizingTableView, which is a table view that
is capable of calculating its intrinsic size.
Use this class for the omnibox popup table view.

Bug: 821817
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Icc01dfcd20ed1f14e15162ac0085f40273747eac
Reviewed-on: https://chromium-review.googlesource.com/973617
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550661}
parent 28a69ac1
...@@ -45,6 +45,8 @@ source_set("popup_internal") { ...@@ -45,6 +45,8 @@ source_set("popup_internal") {
sources = [ sources = [
"omnibox_popup_row.h", "omnibox_popup_row.h",
"omnibox_popup_row.mm", "omnibox_popup_row.mm",
"self_sizing_table_view.h",
"self_sizing_table_view.mm",
] ]
deps = [ deps = [
"//base", "//base",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#import "ios/chrome/browser/ui/omnibox/image_retriever.h" #import "ios/chrome/browser/ui/omnibox/image_retriever.h"
#include "ios/chrome/browser/ui/omnibox/omnibox_util.h" #include "ios/chrome/browser/ui/omnibox/omnibox_util.h"
#import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row.h" #import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row.h"
#import "ios/chrome/browser/ui/omnibox/popup/self_sizing_table_view.h"
#import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h" #import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h"
#include "ios/chrome/browser/ui/rtl_geometry.h" #include "ios/chrome/browser/ui/rtl_geometry.h"
#include "ios/chrome/browser/ui/ui_util.h" #include "ios/chrome/browser/ui/ui_util.h"
...@@ -96,8 +97,9 @@ UIColor* BackgroundColorIncognito() { ...@@ -96,8 +97,9 @@ UIColor* BackgroundColorIncognito() {
} }
- (void)loadView { - (void)loadView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero self.tableView =
style:UITableViewStylePlain]; [[SelfSizingTableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];
self.tableView.delegate = self; self.tableView.delegate = self;
self.tableView.dataSource = self; self.tableView.dataSource = self;
self.view = self.tableView; self.view = self.tableView;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_OMNIBOX_POPUP_SELF_SIZING_TABLE_VIEW_H_
#define IOS_CHROME_BROWSER_UI_OMNIBOX_POPUP_SELF_SIZING_TABLE_VIEW_H_
#import <UIKit/UIKit.h>
// A table view that calculates its intrinsic content height and exposes it
// through intrinsicContentSize property. The intrinsic size is invalidated
// automatically whenever the table view content is changed.
@interface SelfSizingTableView : UITableView
@end
#endif // IOS_CHROME_BROWSER_UI_OMNIBOX_POPUP_SELF_SIZING_TABLE_VIEW_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/omnibox/popup/self_sizing_table_view.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation SelfSizingTableView
#pragma mark - UITableView
- (void)setDelegate:(id<UITableViewDelegate>)delegate {
[super setDelegate:delegate];
[self invalidateIntrinsicContentSize];
}
- (void)reloadData {
[super reloadData];
[self invalidateIntrinsicContentSize];
}
- (void)performBatchUpdates:(void(NS_NOESCAPE ^)())updates
completion:(void (^)(BOOL))completion {
__weak SelfSizingTableView* weakSelf = self;
[super performBatchUpdates:updates
completion:^(BOOL complete) {
if (completion) {
completion(complete);
}
[weakSelf invalidateIntrinsicContentSize];
}];
}
#pragma mark section changes
- (void)reloadSections:(NSIndexSet*)sections
withRowAnimation:(UITableViewRowAnimation)animation {
[super reloadSections:sections withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
- (void)insertSections:(NSIndexSet*)sections
withRowAnimation:(UITableViewRowAnimation)animation {
[super insertSections:sections withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
- (void)deleteSections:(NSIndexSet*)sections
withRowAnimation:(UITableViewRowAnimation)animation {
[super deleteSections:sections withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
#pragma mark row changes
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation {
[super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation {
[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation {
[super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self invalidateIntrinsicContentSize];
}
#pragma mark - UIView
- (CGSize)intrinsicContentSize {
// Add height of every cell together.
CGFloat height = 0;
for (int section = 0; section < [self numberOfSections]; section++) {
for (int row = 0; row < [self numberOfRowsInSection:section]; row++) {
height += [self.delegate tableView:self
heightForRowAtIndexPath:[NSIndexPath indexPathForRow:row
inSection:section]];
}
}
return CGSizeMake(0,
height + self.contentInset.top + self.contentInset.bottom);
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment