CosmicMind/Samples

Add a sample for Card contentView being a UITableView

Closed this issue · 7 comments

I have been having trouble getting UITableView to work inside Card. I tried to modify the Material/Programmatic/Card sample to have UITableView as Card's contentView, but it doesn't seem to scale correctly. Also I would like to get the row selection working. Here's the patch I applied to the sample:

diff --git a/Material/Programmatic/Card/Card/ViewController.swift b/Material/Programmatic/Card/Card/ViewController.swift
index 519921a..0f2a9ca 100644
--- a/Material/Programmatic/Card/Card/ViewController.swift
+++ b/Material/Programmatic/Card/Card/ViewController.swift
@@ -37,7 +37,7 @@ class ViewController: UIViewController {
     fileprivate var toolbar: Toolbar!
     fileprivate var moreButton: IconButton!
     
-    fileprivate var contentView: UILabel!
+    fileprivate var contentView: UITableView!
     
     fileprivate var bottomBar: Bar!
     fileprivate var dateFormatter: DateFormatter!
@@ -93,10 +93,10 @@ extension ViewController {
     }
     
     fileprivate func prepareContentView() {
-        contentView = UILabel()
-        contentView.numberOfLines = 0
-        contentView.text = "Material is an animation and graphics framework that is used to create beautiful applications."
-        contentView.font = RobotoFont.regular(with: 14)
+        contentView = UITableView()
+        contentView.delegate = self
+        contentView.dataSource = self
+        contentView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
     }
     
     fileprivate func prepareBottomBar() {
@@ -124,3 +124,27 @@ extension ViewController {
     }
 }
 
+extension ViewController: UITableViewDelegate {
+    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+        return 60
+    }
+
+    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        print("Hello \(indexPath.row)")
+    }
+}
+
+extension ViewController: UITableViewDataSource {
+    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        return 5
+    }
+
+    /// Prepares the cells within the tableView.
+    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
+        cell.textLabel?.text = "Hello \(indexPath.row)"
+        cell.imageView?.image = Icon.addCircle
+        return cell
+    }
+}
+

I can take a look tomorrow or on Wednesday and help you out :)

I'll be looking at this today :)

Hey, I am putting a sample together for this :)

Thanks a lot for your help.

Any update on this?

Yes, tonight I will be tackling samples. Sorry, so much with the holidays and work, I have been a bit busy.

Hey, I have added a sample that shows how to add a TableView within a Card. It is done programmatically. All the best :)