Reputation: 156
I followed the helpful comments by you people and have currently gotten this far: I have a single TableView with a header cell. The problem now is that I am only able to display one set of data and the TableView will not currently scroll (Maybe because of only one set of data being displayed.)
Here is my Code:
ViewController:
struct TableData {
var section: String = ""
var data = Array<String>()
var dataS = Array<String>()
init(){}
}
var data = Array<TableData>()
var dataS = Array<TableData>()
class MyCustomCell: UITableViewCell {
@IBOutlet var label: UILabel!
@IBOutlet var labelS: UILabel!
}
class MyCustomHeader: UITableViewCell {
@IBOutlet var header: UILabel!
}
class TypeViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
@IBOutlet var scrollView: UIScrollView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].data.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCustomCell
cell.label.text = data[indexPath.section].data[indexPath.row]
cell.labelS.text = dataS[indexPath.section].data[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader
headerCell.header.text = data[section].section
return headerCell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
addItems()
print(data)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addItems() {
var new_elements:TableData
new_elements = TableData()
new_elements.section = "Stuff"
new_elements.data.append(obj41);
new_elements.data.append(obj42);
new_elements.data.append(obj43);
new_elements.data.append(obj44);
new_elements.data.append(obj45);
new_elements.data.append(obj46);
new_elements.data.append(obj47);
data.append(new_elements)
new_elements = TableData()
new_elements.section = "More Stuff"
new_elements.data.append(obj51);
new_elements.data.append(obj52);
new_elements.data.append(obj53);
new_elements.data.append(obj54);
new_elements.data.append(obj55);
new_elements.data.append(obj56);
new_elements.data.append(obj57);
data.append(new_elements)
new_elements = TableData()
new_elements.section = "Netzach - Eternity"
new_elements.data.append(obj61);
new_elements.data.append(obj62);
new_elements.data.append(obj63);
new_elements.data.append(obj64);
new_elements.data.append(obj65);
new_elements.data.append(obj66);
new_elements.data.append(obj67);
data.append(new_elements)
//Break
new_elements = TableData()
new_elements.data.append(objS0);
new_elements.data.append(objS1);
new_elements.data.append(objS2);
new_elements.data.append(objS3);
new_elements.data.append(objS4);
new_elements.data.append(objS5);
new_elements.data.append(objS6);
new_elements.data.append(objS7);
dataS.append(new_elements)
new_elements = TableData()
new_elements.data.append(objS11);
new_elements.data.append(objS12);
new_elements.data.append(objS13);
new_elements.data.append(objS14);
new_elements.data.append(objS15);
new_elements.data.append(objS16);
new_elements.data.append(objS17);
dataS.append(new_elements)
new_elements = TableData()
new_elements.data.append(objS21);
new_elements.data.append(objS22);
new_elements.data.append(objS23);
new_elements.data.append(objS24);
new_elements.data.append(objS25);
new_elements.data.append(objS26);
new_elements.data.append(objS27);
dataS.append(new_elements)
}
Attached are some Photos of the MainStoryboard:
Upvotes: 2
Views: 2177
Reputation: 9503
Keep in mind below important points regarding to UITableView
UITableView
has inherited property from UIScrollView
i.e. UITableView
is also below like a UIScrollView
so you don't need to take UIScrollView
for the specially scroll the UITableView
. If you do it behaves weird.
In cellForRow
, you are creating condition with param tableView
to outlet typeView
& typeView1
by comparing tag which is not a standard format. Because tableView.tag
may be changed and gives you wrong output. So try to use below format
if tableView == typeView { }
else { } //tableView == typeView1
Compare UITableView
objects with pram tableView
.
cellForRow
method returns the cell
from the if-else
so you don't need to write
return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
If I debug your cellForRow
method code then your this above line never executes.
First try this standards and remove your mistake and then post your question and issue you are facing.
Hope my above work helps you.
Edit
Your required output will be like this below image. You don't need to take 2 UItableView
and thse tableview
in single scrollview
. You can do the same with one tableView
.
Go through this tutorial
Upvotes: 2