Skip to content

Instantly share code, notes, and snippets.

@jerrypm
Created October 3, 2018 08:20
Show Gist options
  • Save jerrypm/ee46613245575df529c82d1c62afaa05 to your computer and use it in GitHub Desktop.
Save jerrypm/ee46613245575df529c82d1c62afaa05 to your computer and use it in GitHub Desktop.
//
// CustomTableView.swift
// FoodLibrary
//
// Created by Jerry PM on 03/10/18.
// Copyright © 2018 jeriPm. All rights reserved.
// custom uitableview datasource
import Foundation
import UIKit
class TableViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource {
var data = [String]()
var cell: String?
var didSelectRow: ((_ dataItem: String, _ cell: UITableViewCell) -> Void)?
init(data: [String]) {
self.data = data
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.cell!, for: indexPath)
let text = String(data[indexPath.row])
cell.labelA.text = text
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
let dataItem = data[indexPath.row]
if let didSelectRow = didSelectRow {
didSelectRow(dataItem, cell)
}
}
}
//
// CustomTableView.swift
// FoodLibrary
//
// Created by Jerry PM on 03/10/18.
// Copyright © 2018 jeriPm. All rights reserved.
// custom uitableview datasource
import Foundation
import UIKit
class TableViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource {
var data = [String]()
var data2 = [String]()
var cell: String?
var cell2: String?
var didSelectRow: ((_ dataItem: String, _ cell: UITableViewCell) -> Void)?
init(data: [String], data2: [String]) {
self.data = data
self.data2 = data2
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return data.count
} else {
return data2.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: self.cell!, for: indexPath) as! IATableViewCell
let text = String(data[indexPath.row])
cell.labelA.text = text
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: self.cell2!, for: indexPath) as! IBTableViewCell
let text = String(data2[indexPath.row])
cell.labelB.text = text
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
let dataItem = data[indexPath.row]
if let didSelectRow = didSelectRow {
didSelectRow(dataItem, cell)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment