dropdown tableview - Swift - swift

I draw data from MSSQL and load this data into tableView. No problem with that. I want to make a drop down menu, but I don't know how to do it. Now all the menus are angled. How do I make tables open and close? How can I do this? How do I make a folding tableview?
class CollapsibleTableViewController: UITableViewController {
var sectionNames: [String] = []
var sectionDetay: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
makaleDetay()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
}
func makaleDetay(){
...
client.execute("SELECT soru_icerik FROM uzaktanptomg_db.omg_user.ie_soru", completion: { (_ results: ([Any]?)) in
... self.sectionDetay.append(String(result))
...
}
extension CollapsibleTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return sectionDetay.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ??
CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell")
cell.nameLabel.text = "SSS"
cell.detailLabel.text = sectionDetay[indexPath.section]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
header.titleLabel.text = sectionNames[section]
header.arrowLabel.text = ">"
header.setCollapsed(false)
header.section = section
header.delegate = self
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0
}
}
extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
....
}
}

Related

Multiple headerview in tableview in swift

I want to create 3 headers in the tableview given in the images and 8 students in each row, my code is as follows
Screenshot of the example
class ViewController: UIViewController {
var schoolOfList: [Any] = [
[["school": ["VIP", "Bright International", "Motherland"]],
[["student1": ["Chirag", "Akshay", "Shailesh", "Vishal", "Mehul", "Vinod", "Mukesh", "Darshan"]],
["student2": ["Manjari", "Kairav", "Abhimanyu", "Akshara", "Arohi", "Neel", "Naksh", "Naman"]],
["student3": ["Paritosh", "Anuj", "Kavya", "Samar", "Vanraj", "Kinjal", "Anupama", "Riyan"]]]]]
#IBOutlet weak var mytableview : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> (Int) {
return schoolOfList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
cell.textLabel?.text = schoolOfList["school"] as! [String]indexPath.row
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerview = UIView(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
headerview.backgroundColor = .green
let lable = UILabel(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
lable.textAlignment = .center
lable.text = (schoolOfList["school"]as!String)
headerview.addSubview(lable)
return headerview
}

Swift tableview 'method heightForHeaderInSection' makes section text raise

When heightForHeaderInSection and/or heightForFooterInSection is called, the section text raises above it's first row. I attached a screenshot to show the issue. How can I increase the section spacing without the section header being affected?
#IBOutlet weak var tableView: UITableView!
let fruits =
["apple","pear","orange","caramel","peach","fruit","fruits"]
let numbers = ["1", "2"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
if section == 0 {
return fruits.count
} else {
return numbers.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
if section == 0 {
return "Animals"
} else {
return "Numbers"
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"cell1", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = fruits[indexPath.row]
} else {
cell.textLabel?.text = numbers[indexPath.row]
}
return cell
}
These are the 2 methods in question:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100
}
You are setting the height of the header to 100. See here:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100 // Instead try returning 30
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100 // If you are not using a footer - return .zero
}
I got the answer from another post.
Just add:
tableView.sectionHeaderTopPadding = 100 // or whatever number you want.
There's also another way.
Click on of these ->
You can choose Grouped or the rounded design 'Inset Grouped'
After you can use the method 'heightForHeaderInSection' to select the specific section/s.

TableView Rendering?

I have a table view that is rendering some static data into a UITableView but occasionally when running on a physical device the data is replaced with ? (see screenshot). I have listed the code below if anyone has an idea why this is occurring?
Here is the code for the table view
import UIKit
class OvenViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
extension OvenViewController : UITableViewDataSource,UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return GridData.ovenTempConvArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: OvenCell.className, for: indexPath) as! OvenCell
let data = GridData.ovenTempConvArray[indexPath.row]
cell.lblFTemp.text = "\(data["f"] ?? "") F"
cell.lblCTemp.text = "\(data["c"] ?? "") C"
cell.lblGasMark.text = data["gm"]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}

How to navigate to another view controller from datasource class

I have a UIViewController with a UITableView. I have separate class for UITableViewDataSource and UITableViewDelegate. How can I navigate to another view controller when a cell is selected?
class ViewController: UIViewController {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = TableViewDataSource()
}
}
class TableViewDataSource: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//How to push SecondVC from Viewcontroller?
}
}
class TableViewDataSource: UITableViewDataSource, UITableViewDelegate {
var parentViewController : UIViewController?
func init(parentController : UIViewController){
self.parentViewController = parentController
}
// rest of your code
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//How to push SecondVC from Viewcontroller?
let storyboard = UIStoryboard(name: "Stroybaordname", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "ViewControllerName")
parentViewController.present(secondVC, animated: true, completion: nil)
}
}
Try this way
If I had to do this I would do it using a separate delegate method. If I am missing the context pardon me.
class ViewController: UIViewController, TableViewDataSourceDelegate {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
let customTableDataSource = TableViewDataSource()
customTableDataSource.delegate = self
// Setting the custom delegate
tableView.delegate = customTableDataSource
}
func cellSelected(at: IndexPath) {
// Navigate from here.
}
}
/// Protocol helps to communicate with the TableViewDataSource to the user of it.
protocol TableViewDataSourceDelegate {
func cellSelected(at: IndexPath)
}
class TableViewDataSource: UITableViewDataSource, UITableViewDelegate {
var delegate: TableViewDataSourceDelegate?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.cellSelected(at: indexPath)
}
}
You can create a blocks to access it in your ViewController class.
//MARK:- MODULES
import UIKit
//MARK:- TYPEALIAS
typealias ListCellConfigureBlock = (_ cell : AnyObject? , _ item : AnyObject? , _ indexpath: IndexPath?) -> ()
typealias DidSelectedRow = (_ indexPath : IndexPath) -> ()
typealias ScrollViewDidScroll = (_ scrollView : UIScrollView) -> ()
typealias ViewForHeaderInSection = (_ section : Int) -> UIView?
typealias DidDeselectedRow = (_ indexPath : IndexPath) -> ()
typealias CanEditRowAt = (_ indexPath : IndexPath) -> Bool
typealias CommitEditingStyle = (_ commiteditingStyle: UITableViewCellEditingStyle, _ indexPath : IndexPath) -> ()
typealias HeightForRowAt = (_ indexPath : IndexPath) -> CGFloat
//MARK:- CLASS
class TableViewDataSource: NSObject {
//MARK:- PROPERTIES
var _section: [String]?
var sectionCount: Array<AnyObject>?
var rowCount : Array<AnyObject>?
var cellIdentifier : String?
var tableView : UITableView?
var configureCellBlock : ListCellConfigureBlock?
var aRowSelectedListener : DidSelectedRow?
var ScrollViewListener : ScrollViewDidScroll?
var viewforHeaderInSection : ViewForHeaderInSection?
var headerHeight : CGFloat?
var aRowDeselectedListener : DidDeselectedRow?
var aRowEditListener : CanEditRowAt?
var aRowCommitListener : CommitEditingStyle?
var aRowHeightListener : HeightForRowAt?
init (tableView : UITableView?, cellIdentifier : String?, sectionCount: Array<AnyObject>?, rowCount : Array<AnyObject>? , height : HeightForRowAt? , configureCellBlock : ListCellConfigureBlock? , aRowSelectedListener : #escaping DidSelectedRow,aRowDeselectedListener: #escaping DidDeselectedRow ,aRowCommitListener: #escaping CommitEditingStyle, DidScrollListener : ScrollViewDidScroll?) {
self.tableView = tableView
self.sectionCount = sectionCount
self.rowCount = rowCount
self.cellIdentifier = cellIdentifier
self.aRowHeightListener = height
self.configureCellBlock = configureCellBlock
self.aRowSelectedListener = aRowSelectedListener
self.aRowDeselectedListener = aRowDeselectedListener
self.aRowCommitListener = aRowCommitListener
self.ScrollViewListener = DidScrollListener
}
override init() {
super.init()
}
}
//MARK:- DELEGATE, DATASOURCE
extension TableViewDataSource : UITableViewDelegate , UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let identifier = cellIdentifier else { fatalError("Cell identifier not provided") }
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: identifier , for: indexPath) as UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
if let block = self.configureCellBlock , let item: AnyObject = self.rowCount?[(indexPath as NSIndexPath).row]{
block(cell , item , indexPath as IndexPath?)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let block = self.aRowSelectedListener{
block(indexPath)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rowCount?.count ?? 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.sectionCount?.count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let block = self.aRowHeightListener else { return 0.0 }
return block(indexPath)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let block = viewforHeaderInSection else { return nil }
return block(section)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self._section?[section]
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return headerHeight ?? 0.0
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let block = self.ScrollViewListener{
block(scrollView)
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let block = self.aRowDeselectedListener {
block(indexPath)
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
guard let block = self.aRowEditListener else { return false }
return block(indexPath)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if let block = self.aRowCommitListener{
block(editingStyle, indexPath)
}
}
}
You can use it like:
//Create a variable:
var dataSource = TableViewDataSource()
fileprivate func setUpDataSource() throws {
DispatchQueue.main.async { [weak self] in
dataSource = TableViewDataSource.init(tableView: tblView, cellIdentifier: CellId.chatUserTVCell.rV, sectionCount: arrSection as Array<AnyObject>, rowCount: arrSearchChatListing , height: { (indexPath) -> CGFloat in
return 80.0
}, configureCellBlock: { (cell, item, indexPath) in
guard let _cell = cell as? ChatUserTableCell, let _item = item as? ChatListing, let row = indexPath?.row else { return }
_cell.chatListing = _item
try? self?.shouldRequestPaging(row: row)
}, aRowSelectedListener: { (indexPath) in
let vc = StoryboardScene.Home.instantiateChatTextController()
try? self?.pushVC(vc)
}, aRowDeselectedListener: { (indexPath) in
debugPrint("Deselected")
}, aRowCommitListener: { (editingStyle, indexPath) in
debugPrint("aRowCommitListener")
}, DidScrollListener: { (scrollView) in
debugPrint("DidScrollListener")
})
tblView.delegate = dataSource
tblView.dataSource = dataSource
tblView.reloadData()
}
}
Hope it helps :)

UITableView inside UIViewController

I'm trying to use UITableView Inside UIViewController. However, when I tried to so it it gives me an error when I start the app.
the error says "method doesnt override ant method from superclass"
import UIKit
class GPATableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectsArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
objectsArray =
[Objects(sectionName: "Section1" , sectionObjects: ["","",""]),
Objects(sectionName: "Section2" , sectionObjects: ["","",""]),
Objects(sectionName: "Section3" , sectionObjects: ["","",""])]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
// cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
print
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
}
When you implement the UITableViewDatasource in an UIViewController, you are not overriding the methods.
Remove override from the methods that the compiler is tell you to.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
// cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
print
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
NOTE: If you were using a UITableViewController, then you would need the override, which is probably what was going on in whatever file you copied this from.
Functions involving tableView such as numberOfSections(...), cellForRowAt etc. are not part of the UIViewController.
Instead, they belong to the UITableViewDelegate and UITableViewDataSource.
To fix your errors, remove the override keyword in front of these functions. You are not overriding them but instead "implementing" them at the request of the protocols.
Be sure to also set the view controller as the delegate and dataSource if you have not already done so in a Storyboard:
override func viewDidLoad()
super.viewDidLoad()
// other code...
tableView.delegate = self
tableView.dataSource = self
// other code...
}