TableView does not reload data - ios

I am simply trying reload a TableView with new items pulled from an array and the tableView does not reload the new items. Here is the code. Any help please?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var myTableView: UITableView!
#IBAction func reloadData(sender: UIButton) {
tableData = newItems
self.tableViewController.tableView.reloadData()
}
var items = ["AAA", "BBB"]
var newItems = ["XXX", "YYY"]
var tableData = [String]()
var tableViewController = UITableViewController(style: .Plain)
override func viewDidLoad() {
super.viewDidLoad()
tableData = items
var tableView = tableViewController.tableView
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell
}
}

There are many mistakes in your code and here is your complete working code:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var myTableView: UITableView!
var items = ["AAA", "BBB"]
var newItems = ["XXX", "YYY"]
var tableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableData = items
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
myTableView.delegate = self
myTableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
#IBAction func reloadData(sender: AnyObject) {
tableData = newItems
self.myTableView.reloadData()
}
}
Compare your code with this code and you will understand what you have done wrong.
HERE is sample project for more info.

import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var myTableView: UITableView!
var flag=1
var items = ["AAA", "BBB"]
var newItems = ["XXX", "YYY"]
var tableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
if flag==1{
tableData = items
}
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
myTableView.delegate = self
myTableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
#IBAction func reloadData(sender: AnyObject) {
if flag==1{
tableData = items
flag=0
}else
{
tableData=newItems
flag=1
}
self.myTableView.reloadData()
}
}

import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var myTableView: UITableView!
//var flag=1
var items = ["AAA", "BBB"]
var newItems = ["XXX", "YYY"]
var tableData = String
override func viewDidLoad() {
super.viewDidLoad()
// if flag==1{
// tableData = items
// }
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
myTableView.delegate = self
myTableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
#IBAction func reloadData(sender: AnyObject) {
// if flag==1{
// tableData = items
// flag=0
// }else
// {
// tableData=newItems
// flag=1
// }
self.myTableView.reloadData()
}
}

Related

TableView not display the array datas

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var myTableView: UITableView!{
didSet {
tableView.dataSource = self
//tableView.reloadData()
}
}
var data = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
data = ["data1","data2", "data3"]
//tableView.reloadData()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.lbl?.text = data[indexPath.row]
return cell
}
}
you need to set delegate and you have not used code properly.
#IBOutlet var myTableView: UITableView!
var data:[String]!{
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
data = ["data1","data2", "data3"]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.lbl?.text = data[indexPath.row]
return cell
}
}
Note: if your cell is a nib file then register your cell in viewDidLoad before setting delegate=self
tableView.register(UINib(nibName: "nibname", bundle: nil), forCellReuseIdentifier: "myCell")
To get you going, this is all the code you need:
class MyCell: UITableViewCell {
#IBOutlet var lbl: UILabel!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var myTableView: UITableView!
var data = [String]()
override func viewDidLoad() {
super.viewDidLoad()
data = ["data1","data2", "data3"]
// set the table view's dataSource
myTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.lbl?.text = data[indexPath.row]
return cell
}
}
This assumes that you
created a cell prototype in Storyboard
assigned its Custom Class to MyCell
gave it an identifier of "myCell"
added a label to it and connected it to #IBOutlet var lbl: UILabel!
Move
tableView.dataSource = self
to ViewDidLoad()
And add
tableView.delegate = self
Below dataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

swift - Dynamic List of items inside tableViewCell

What is the best practice to add dynamic list of items in UITableViewCell with showMore/showLess?
UITableView inside UITableViewCell
I used table view inside my UITableViewCell , but I have issue with reload row at indexPath causes jumpy scrolling.
TableViewController Code :
class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource , delegatCell {
#IBOutlet weak var tableView: UITableView!
var elementsArray: [data] = []
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...20 {
elementsArray.append(data.init(title: "title\(i)", date: "8:00 PM", isShowMoreClicked: false, elements: ["ToDo\(i)","Aqraa\(i)","ToDo\(i)","ToDo\(i)" , "Mobile Team\(i)" , "Testing Data\(i)"]))
// Do any additional setup after loading the view, typically from a nib.
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elementsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let obj = elementsArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! expandedTableViewCell
cell.initializeCell()
print(obj.elements)
cell.data = obj.elements
cell.delegate = self
cell.dateLabel.text = obj.date
cell.titleLabel.text = obj.title
cell.showMore.titleLabel?.font = reqularDynamicFont()
cell.showMore.titleLabel?.adjustsFontSizeToFitWidth = true
cell.showMore.tag = indexPath.row
cell.isShowMoreClicked = obj.isShowMoreClicked
cell.tableView.reloadData()
self.view.layoutIfNeeded()
return cell
}
func reqularDynamicFont()-> UIFont{
let font = UIFont.systemFont(ofSize: 15, weight: .regular)
let fontMetrics = UIFontMetrics(forTextStyle: .caption1)
return fontMetrics.scaledFont(for: font)
}
func reloadTableView(indexpath: IndexPath , isShowMoreClicked: Bool) {
elementsArray[indexpath.row].isShowMoreClicked = isShowMoreClicked
tableView.reloadRows(at: [indexpath], with: .automatic)
}
}
ExpandedTableViewCell with inner table view Code :
import UIKit
protocol delegatCell {
func reloadTableView(indexpath: IndexPath , isShowMoreClicked: Bool)
}
class expandedTableViewCell: UITableViewCell , UITableViewDelegate , UITableViewDataSource{
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var showMore: UIButton!
var data : [String] = []
var initalNum = 2
var isShowMoreClicked: Bool = false
var delegate:delegatCell?
func initializeCell(){
data.removeAll()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isShowMoreClicked{
return data.count
}else{
return initalNum
}
}
#IBAction func reloadData(_ sender: UIButton) {
delegate?.reloadTableView(indexpath: IndexPath(row: sender.tag, section: 0), isShowMoreClicked: !isShowMoreClicked)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(data)
let cell = tableView.dequeueReusableCell(withIdentifier: "simpleTableViewCell", for: indexPath) as! simpleTableViewCell
cell.descLabel.text = data[indexPath.row]
return cell
}
}
I used auto layout to support dynamic height.

How to navigate from MasterView's TableViewCell (Xib Cell) to DetailView's TableView

I have a master-detail view application in which MasterView's table cell is a Xib cell. I also have two TableViews in DetailViewController placed side by side as shown below:
Master and Detail Views
Now my problem is I am unable to populate DetailView's Table View when I am clicking on Master's TableViewCell. I am pasting my code below:
class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var devarlViewController: DetailViewController? = nil
var objects = [Any]()
var statesList = ["AL", "GA", "AK", "AR", "AZ", "CA", "CO", "CT", "CO"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view, typically from a nib.
}
// MARK: - Table View
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return statesList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = statesList[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
DetailViewController.sharedDetail.selectedState = (self.tableView(tableView, cellForRowAt: indexPath).textLabel?.text)!
DetailViewController.sharedDetail.selectedCity = ""
}
}
class DetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
static let sharedDetail = DetailViewController()
#IBOutlet weak var CitiesTable: UITableView!
#IBOutlet weak var PlacesTable: UITableView!
var selectedState: String = ""{
didSet{
CitiesTable.reloadData()
}
}
var selectedCity: String = ""
var citiesStates: [String:[String]] = [ "AL": ["Auburn", "Montgomery", "Birmingham"]
]
var placesCities: [String:[String]] = [ "Auburn": ["Dominos", "Pizzahut", "McDonalds"],
"Birmingham": ["iHop", "Coldstone", "WaffleHouse"]
]
var cities: [String] = []
var places: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(tableView == CitiesTable)
{
if(selectedState != "")
{
cities = citiesStates[selectedState]!
return cities.count
}
}
else
{
if(selectedCity != "")
{
places = placesCities[selectedCity]!
return places.count
}
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(tableView == CitiesTable)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "citiesCell", for: indexPath)
cell.textLabel?.text = cities[indexPath.row]
return cell
}
else
{
let cell = tableView.dequeueReusableCell(withIdentifier: "placesCell", for: indexPath)
cell.textLabel?.text = cities[indexPath.row]
return cell
}
}
}
I have added a protocol in masterviewcontroller shown below
protocol MasterSelectionDelegate: class {
func itemSelected(_ item: AnyObject)
}
then, as splitviewcontroller sits as a rootviewcontroller.
In app delegate or during creation of Split View Controller. I have added below line
masterViewController.delegate = detailViewController
In didSelectRowAtIndexPath of master ViewController I have called it this:
`override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = self.arrString[indexPath.row]
self.delegate?.itemSelected(selectedItem)
}`
Finally you have to implement it in the delegate in detail view controller
func itemSelected(_ item: AnyObject) {
itemSelected = item as? Array<String>
tableview.reloadData()
}

tableview cell in swift3

As I am beginer in iOS swift3.0
I am trying to create simple table view using xib cell in swift3.I have gone through some tutorials but I cont find the correct example. so can some one help me in it.
please find my below code
class Myclass: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tabelviewoutlet: UITableView!
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellIdentifier = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "td")! as UITableViewCel
}
please find the code and let me know.
import UIKit
class ViewController: UIViewController,UITableViewDataSource{
#IBOutlet weak var tabelviewoutlet: UITableView!
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellIdentifier = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
tabelviewoutlet.dataSource = self
tabelviewoutlet.register(UINib(nibName: "aTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return animals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! aTableViewCell
//cell.albl?.text = self.animals[indexPath.row]
cell.albl.text=self.animals[indexPath.row]
return cell
}
}
Use code like this,
class Myclass: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tabelviewoutlet: UITableView!
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
override func viewDidLoad() {
super.viewDidLoad()
// register your xib
self.tblMyCustom.register(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "customCell")
self.tblMyCustom.delegate = self
self.tblMyCustom.datasource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let customCell: CustomTableCell! = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomTableCell
customCell.myLabel.text = self.animals[indexPath.row]
return customCell
}
}

Add a picker view at the footer of the app

I would like to add a picker view at the footer of the app when I tap the cell and change the Detail label with the picker view data. I would like to do the same think with the DatePicker, but only in one cell ( the one named Service appt. Date).
This is my code:
import UIKit
class Service2Appointment: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var TableView: UITableView!
#IBOutlet weak var pickerView: UIPickerView!
let FormName = ["State","City","Vehicle Reg Number","Location","Current Kms","Service appt. Date","Service appt. Type","Pick-up Vehicle","Discount Code","Service Notes"]
let textCellIdentifier = "TextCell"
override func viewDidLoad() {
super.viewDidLoad()
pickerView.hidden = true
TableView.delegate = self
TableView.dataSource = self
self.TableView.rowHeight = 70
TableView.backgroundView = UIImageView(image: UIImage(named: "background.png"))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FormName.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = TableView.dequeueReusableCellWithIdentifier(FormName[indexPath.row], forIndexPath: indexPath) as! UITableViewCell
if (indexPath.row % 2 == 0) {
cell.backgroundColor = UIColor.clearColor()
}else{
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
}
let row = indexPath.row
cell.detailTextLabel?.text = FormName[row]
cell.textLabel?.text = FormName[row]
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}
// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println(FormName[row])
}
}

Resources