Escher: TableView reuses cells

Change-Id: Icdbfafba351057affc2bcb74f9c967971ffee667
This commit is contained in:
Romain Goyet
2016-06-14 10:52:03 +02:00
parent 2586b8d2b4
commit 63ed8207ce
2 changed files with 12 additions and 6 deletions

View File

@@ -6,8 +6,8 @@
class TableViewDataSource {
public:
virtual int numberOfCells() = 0;
virtual View * cellAtIndex(int index) = 0;
virtual void willDisplayCellAtIndex(int index) = 0;
virtual View * reusableCell(int index) = 0;
virtual void willDisplayCellForIndex(View * cell, int index) = 0;
virtual KDCoordinate cellHeight() = 0;
};
@@ -28,9 +28,10 @@ private:
void layoutSubviews() override;
protected:
#if ESCHER_VIEW_LOGGING
const char * className() const override;
const char * className() const override;
#endif
private:
int numberOfDisplayableCells() const;
int cellScrollingOffset();
TableViewDataSource * m_dataSource;
};

View File

@@ -30,13 +30,14 @@ TableView::ContentView::ContentView(TableViewDataSource * dataSource) :
}
int TableView::ContentView::numberOfSubviews() const {
int numberOfDisplayableCells = m_frame.height / m_dataSource->cellHeight();
return MIN(m_dataSource->numberOfCells(), numberOfDisplayableCells);
return MIN(m_dataSource->numberOfCells(), numberOfDisplayableCells());
}
View * TableView::ContentView::subview(int index) {
int offset = cellScrollingOffset();
return m_dataSource->cellAtIndex(offset+index);
View * cell = m_dataSource->reusableCell(index);
m_dataSource->willDisplayCellForIndex(cell, offset+index);
return cell;
}
void TableView::ContentView::storeSubviewAtIndex(View * view, int index) {
@@ -59,6 +60,10 @@ void TableView::ContentView::layoutSubviews() {
}
}
int TableView::ContentView::numberOfDisplayableCells() const {
return m_frame.height / m_dataSource->cellHeight();
}
// Index of the topmost cell
int TableView::ContentView::cellScrollingOffset() {
/* Here, we want to translate the offset at which our superview is displaying