Skip to content

Instantly share code, notes, and snippets.

@paulw11
Created February 6, 2017 01:34
Show Gist options
  • Save paulw11/30c0ed7dd19be183d38b96baa646d13b to your computer and use it in GitHub Desktop.
Save paulw11/30c0ed7dd19be183d38b96baa646d13b to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// TwoTables
//
// Created by Paul Wilkinson on 6/2/17.
// Copyright © 2017 Paul Wilkinson. All rights reserved.
//
import UIKit
struct TableData {
var section: String = ""
var data = Array<String>()
var dataS = Array<String>()
init(){}
}
class MyCustomCell: UITableViewCell {
@IBOutlet var label: UILabel!
@IBOutlet var labelS: UILabel!
}
class MyCustomWordCell: UITableViewCell {
@IBOutlet var wordLabel: UILabel!
@IBOutlet var wordLabelS: UILabel!
}
class MyCustomHeader: UITableViewCell {
@IBOutlet var header: UILabel!
}
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {
var data = Array<TableData>()
var dataS = Array<TableData>()
var wordData = TableData()
@IBOutlet var tableView: UITableView!
@IBOutlet var wordTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return data[section].data.count
}
if tableView == self.wordTableView {
return wordData.data.count
}
return 0
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "TypeCell", for: indexPath) as! MyCustomCell
cell.label.text = data[indexPath.section].data[indexPath.row]
cell.labelS.text = dataS[indexPath.section].dataS[indexPath.row]
return cell
}
if tableView == self.wordTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath) as! MyCustomWordCell
cell.wordLabel.text = wordData.data[indexPath.row]
cell.wordLabelS.text = wordData.dataS[indexPath.row]
return cell
}
return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
}
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return data.count
} else {
return 1
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == self.tableView {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader
headerCell.header.text = data[section].section
return headerCell
}
return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Header")
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableView {
wordData.data.append(data[indexPath.section].data[indexPath.row]);
wordData.dataS.append(dataS[indexPath.section].dataS[indexPath.row]);
wordTableView.reloadData()
/* if wordData.count == 1 {
performSegue(withIdentifier: "segueFind", sender: self)
}*/
}
if tableView == self.wordTableView {
wordData.data.remove(at: indexPath.row)
wordData.dataS.remove(at: indexPath.row)
wordTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
addItems()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addItems() {
var new_elements:TableData
//Code Declaring the objects has been removed due to Privacy Issues
new_elements = TableData()
new_elements.section = "Section 0 - Section"
new_elements.data.append("obj1");
new_elements.data.append("obj2");
new_elements.data.append("obj3");
new_elements.data.append("obj4");
new_elements.data.append("obj5");
new_elements.data.append("obj6");
new_elements.data.append("obj7");
data.append(new_elements)
new_elements = TableData()
new_elements.section = "Section 01 - Section"
new_elements.data.append("obj11");
new_elements.data.append("obj12");
new_elements.data.append("obj13");
new_elements.data.append("obj14");
new_elements.data.append("obj15");
new_elements.data.append("obj16");
new_elements.data.append("obj17");
data.append(new_elements)
//Break
new_elements = TableData()
new_elements.section = "Section - Section"
new_elements.dataS.append("objS1");
new_elements.dataS.append("objS2");
new_elements.dataS.append("objS3");
new_elements.dataS.append("objS4");
new_elements.dataS.append("objS5");
new_elements.dataS.append("objS6");
new_elements.dataS.append("objS7");
dataS.append(new_elements)
new_elements = TableData()
new_elements.section = "Section1 - Section"
new_elements.dataS.append("objS11");
new_elements.dataS.append("objS12");
new_elements.dataS.append("objS13");
new_elements.dataS.append("objS14");
new_elements.dataS.append("objS15");
new_elements.dataS.append("objS16");
new_elements.dataS.append("objS17");
dataS.append(new_elements)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment