Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Working on UITableView
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 18, 2018
1 parent bfda578 commit 8c0cbe1
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 33 deletions.
32 changes: 25 additions & 7 deletions Sources/Cacao/UITableView.swift
Expand Up @@ -13,7 +13,13 @@
#endif

import Foundation

#if os(iOS)
import UIKit
import CoreGraphics
#else
import Silica
#endif

/// A view that presents data using rows arranged in a single column.
open class UITableView: UIScrollView {
Expand All @@ -38,6 +44,12 @@ open class UITableView: UIScrollView {
self.init(frame: frame, style: .plain)
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

// MARK: - Providing the Table View Data

/// The object that acts as the data source of the table view.
Expand Down Expand Up @@ -298,7 +310,7 @@ open class UITableView: UIScrollView {

if rect.intersects(simpleRowRect) {

results.append(IndexPath(row: row, in: sectionIndex))
results.append(IndexPath(row: row, section: sectionIndex))

} else if pastEnd {

Expand All @@ -325,7 +337,7 @@ open class UITableView: UIScrollView {
/// each representing a visible cell in the table view.
public var visibleCells: [UITableViewCell] {

return indexPathsForVisibleRows?.flatMap { cellForRow(at: $0) } ?? []
return indexPathsForVisibleRows?.compactMap { cellForRow(at: $0) } ?? []
}

/// An array of index paths each identifying a visible row in the table view.
Expand Down Expand Up @@ -674,7 +686,7 @@ open class UITableView: UIScrollView {

internal static let defaultHeaderFooterHeight: CGFloat = 22

internal static let defaultSeparatorColor = UIColor(red: 0.88, green: 0.88, blue: 0.88)
internal static let defaultSeparatorColor = UIColor(red: 0.88, green: 0.88, blue: 0.88, alpha: 1.0)

internal static let defaultSeparatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)

Expand Down Expand Up @@ -832,7 +844,7 @@ open class UITableView: UIScrollView {

for row in 0 ..< numberOfRowsInSection {

let rowHeight = _delegate.tableView(self, heightForRowAt: IndexPath(row: row, in: sectionIndex))
let rowHeight = _delegate.tableView(self, heightForRowAt: IndexPath(row: row, section: sectionIndex))
section.rowHeights[row] = rowHeight
}

Expand Down Expand Up @@ -884,7 +896,7 @@ open class UITableView: UIScrollView {
for row in 0 ..< section.numberOfRows {

// create index path for row in section
let indexPath = IndexPath(row: row, in: sectionIndex)
let indexPath = IndexPath(row: row, section: sectionIndex)

// get layout rect for row
let rowRect = rectForRow(at: indexPath)
Expand Down Expand Up @@ -1058,14 +1070,20 @@ private extension UITableView {

init(title: String) {
super.init(frame: CGRect())
self.font = UIFont.boldSystemFontOfSize(17)
self.font = UIFont.boldSystemFont(ofSize: 17)
self.textColor = .white
self.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 0.8)
//self.shadowColor = UIColor(red: (100 / 255.0), green: (105 / 255.0), blue: (110 / 255.0))
//self.shadowOffset = CGSize(width: 0, height: 1)
}

override func draw(_ rect: CGRect?) {
#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

override func draw(_ rect: CGRect) {
/*
let size: CGSize = bounds.size
UIColor(red: CGFloat(166 / 255.0), green: CGFloat(177 / 255.0), blue: CGFloat(187 / 255.0), alpha: CGFloat(1)).setFill()
Expand Down
41 changes: 37 additions & 4 deletions Sources/Cacao/UITableViewCell.swift
Expand Up @@ -7,6 +7,13 @@

import Foundation

#if os(iOS)
import UIKit
import CoreGraphics
#else
import Silica
#endif

/// A cell in a table view.
///
/// This class includes properties and methods for setting and managing cell content
Expand All @@ -30,6 +37,12 @@ open class UITableViewCell: UIView {
self.setupTableViewCellCommon()
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

// MARK: - Reusing Cells

/// A string used to identify a cell that is reusable.
Expand Down Expand Up @@ -326,6 +339,12 @@ internal final class UITableViewCellContentView: UIView {
self.tableViewCellContentViewCommonSetup()
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

private func tableViewCellContentViewCommonSetup() {

// should only be called once
Expand All @@ -342,9 +361,11 @@ internal final class UITableViewCellContentView: UIView {
let contentSubviews: [UIView?] = [textLabel, detailTextLabel, imageView]

// add as subviews to content view
contentSubviews
.flatMap { $0 }
.forEach { self.addSubview($0) }
contentSubviews.forEach {
if let view = $0 {
self.addSubview(view)
}
}

// set properties
self.textLabel = textLabel
Expand Down Expand Up @@ -419,6 +440,12 @@ internal final class _UITableViewCellSeparatorView: UIView {
self.isHidden = true
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

override func draw(_ rect: CGRect) {

guard let context = UIGraphicsGetCurrentContext(),
Expand Down Expand Up @@ -595,7 +622,7 @@ internal class UITableViewLabel: UILabel {

private(set) weak var cell: UITableViewCell!

override var text: String {
override var text: String? {

didSet {

Expand All @@ -611,6 +638,12 @@ internal class UITableViewLabel: UILabel {

self.cell = cell
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif
}

// MARK: - ReusableView Protocol
Expand Down
12 changes: 12 additions & 0 deletions Sources/Cacao/UITableViewController.swift
Expand Up @@ -6,7 +6,13 @@
//

import Foundation

#if os(iOS)
import UIKit
import CoreGraphics
#else
import Silica
#endif

/// A controller object that manages a table view.
open class UITableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
Expand All @@ -28,6 +34,12 @@ open class UITableViewController: UIViewController, UITableViewDataSource, UITab
super.init(nibName: nil, bundle: nil)
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

// MARK: - Getting the Table View

/// Returns the table view managed by the controller object.
Expand Down
13 changes: 13 additions & 0 deletions Sources/Cacao/UITableViewHeaderFooterView.swift
Expand Up @@ -7,6 +7,13 @@

import Foundation

#if os(iOS)
import UIKit
import CoreGraphics
#else
import Silica
#endif

/// A reusable view that can be placed at the top or bottom of a table section
/// to display additional information for that section.
///
Expand All @@ -25,6 +32,12 @@ open class UITableViewHeaderFooterView: UIView {
super.init(frame: .zero)
}

#if os(iOS)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#endif

// MARK: - Accessing the Content Views

/// The content view of the header or footer.
Expand Down
2 changes: 1 addition & 1 deletion Sources/CacaoDemo/AppDelegate.swift
Expand Up @@ -36,7 +36,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate {
}

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = ScrollViewController()
self.window?.rootViewController = TableViewController()
self.window?.makeKeyAndVisible()

return true
Expand Down
49 changes: 29 additions & 20 deletions Sources/CacaoDemo/TableViewController.swift
Expand Up @@ -8,36 +8,53 @@
import Foundation

#if os(iOS) || os(tvOS)
import UIKit
import CoreGraphics
import UIKit
import CoreGraphics
#else
import Cacao
import Silica
//import Cacao
import Silica
#endif

final class TableViewController: UITableViewController {

private let data = Array((1...100))
// MARK: - Properties

private var data = Array((1...5)) {

didSet { tableView.reloadData() }
}

private let cellReuseIdentifier = "Cell"

// MARK: - Loading

override func viewDidLoad() {
super.viewDidLoad()

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

#if os(iOS)
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
if #available(iOS 11.0, *) {
((tableView as NSObject) as? UIKit.UIScrollView)?.contentInsetAdjustmentBehavior = .never
}
#endif

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

//tableView.estimatedRowHeight = 44
//tableView.rowHeight = UITableViewAutomaticDimension

DispatchQueue.main.asyncAfter(deadline: .now() + 5) { [weak self] in
self?.data = Array((1...100))
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let _ = self.tableView.becomeFirstResponder()

}

// MARK: - UITableViewDataSource

override func numberOfSections(in tableView: UITableView) -> Int {

return 1
Expand All @@ -57,6 +74,8 @@ final class TableViewController: UITableViewController {
return cell
}

// MARK: - Methods

private func configure(cell: UITableViewCell, at indexPath: IndexPath) {

let item = self[indexPath]
Expand All @@ -70,14 +89,4 @@ final class TableViewController: UITableViewController {

return "test \(number)"
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {

print("Did scroll")
}

override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

print("Will begin dragging")
}
}

0 comments on commit 8c0cbe1

Please sign in to comment.