UITableView show some row - ios

I have a problem with UITableView in UIViewController.My table shows some row that I don't know why. It should be show all of row.
class TableViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
let cellIdentifier = "testCell"
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 20
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell
cell.lableName.text = "Hello man"
return cell
}
}

if you want hello man in 20 row.(All row)in cell for row at index path method add this line
cell.labelname.text = "Hello Man"

Please check your tableview delegate and datasource.
BTW,set right constraints.

Related

How do i create a UItableViewContoller with Static Cells and insert a UITableView with Prototype Cell into one of those Static Cells?

is it possible to accomplish? even if it's not recommended
if it is possible, how do i do it exactly?
if you know a way - please elaborate as much as possible :)
Im working on Swift, so Obj-C doesn't really help
Thank you to all those who take the time to read and answer
First starting with the storyboard here is the hierarchy of tableViewController. see the below image.
after that create a UITableViewCell class to hold the second table view and assign that class to second table view cell. as below.
import UIKit
class tableTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
tableView.delegate = self
tableView.dataSource = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("dynamicCell")!
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
and then in tableViewController cellForRow method initialize both the cell and return the cell as required.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 2
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 50
} else {
return 200
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("tableCell")! as! tableTableViewCell
return cell
}
// Configure the cell...
}
That's It. here you Go. Here is the output of the above code.
You can see that first "cell 1" is a static cell and below that in second cell there is another tableView showing the number 0-4 means another 5 cell of second tableview in cell 2.

Adding TableViewCell Section Header everytime a new cell is created that displays month and year created - swift

I'm trying to display a section header with the month and year the cell was created as the text in the section header. This is my code but it only displays one section header like so. Any idea why and how I can get it to display the year and month the cell was created?
import UIKit
class PRViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblTasks : UITableView!
//For persisting data
let defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
self.tblTasks.reloadData()
tblTasks.registerNib(UINib(nibName: "PRTableViewCell", bundle: nil), forCellReuseIdentifier: "PRTableCell")
tblTasks.tableFooterView = UIView()
}
override func viewWillAppear(animated: Bool) {
self.tblTasks.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your PR's"
}
//Define how our cells look - 2 lines a heading and a subtitle
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let identifier = "PRTableCell"
var cell: PRTableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? PRTableViewCell
if cell == nil {
tableView.registerNib(UINib(nibName: "PRTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? PRTableViewCell
}
// Assign the contents of our var "items" to the textLabel of each cell
// cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
// cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc
cell.PRLabel.text = taskMgr.tasks[indexPath.row].name
cell.NotesLabel.text = taskMgr.tasks[indexPath.row].desc
cell.WeightLabel.text = taskMgr.tasks[indexPath.row].weight + "lb"
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete){
taskMgr.removeTask(indexPath.row)
tblTasks.reloadData()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
let numberOfSections = taskMgr.tasks.count
return numberOfSections
}
}
Here's one way to do it. Note that this code assumes your cell data is in an array called "cellArray". It displays a date that starts with today and goes back one day for each section. Obviously you'll need to substitute your dates.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.cellArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
// TODO: configure cell for display using self.cellArray[indexPath.section]
return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .NoStyle
// TODO: determine the actual date
let displayDate = NSDate().dateByAddingTimeInterval(Double(section * -86400))
return df.stringFromDate(displayDate)
}
So example code is below (code not tested obviously). I have assumed that there is a dateCreated property containing an NSDate in the objects held in your tasks array.
Example code:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let formatter = NSDateFormatter() // This is slightly inefficient for a large number of rows because setting up NSDateFormatter is expensive. You could make this a property.
formatter.dateStyle = .ShortStyle
formatter.timeStyle = .NoStyle
let sectionHeaderDate = taskMgr.tasks[section].dateCreated
let dateString = formatter.stringFromDate(sectionHeaderDate)
return dateString
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "PRTableCell"
var cell: PRTableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath:idx) as? PRTableViewCell // You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method (http://stackoverflow.com/q/12737860/558933).
cell.PRLabel.text = taskMgr.tasks[indexPath.row].name
cell.NotesLabel.text = taskMgr.tasks[indexPath.row].desc
cell.WeightLabel.text = taskMgr.tasks[indexPath.row].weight + "lb" // Note iOS 9 allows you to localise weights rather than hard-coding "lb" or "kg". You should look at the documentation.
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
let numberOfSections = taskMgr.tasks.count
return numberOfSections
}
To add a new "section" to the UITableView you must add the new data to the array taskMgr.tasks and then either reload the table or update just the added rows. Wrap these lines of code in tblTasks.beginUpdates and tblTasks.endUpdates. Similarly for deleting.

Add a row before rendering tableview swift 2

I am trying to add a row into my tableview before the tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int method is called. I tried to do it into the viewDidLoad() method without success is it possible ? and how ?
Here is my code :
import UIKit
class CustomTestViewController : UITableViewController {
#IBOutlet var formDetailTableView: UITableView!
var data: Data?
var firstData: FirstData?
struct CurrentFormTableView {
struct CellIdentifiers {
static let MyCell = "MyCell"
static let MyFirstCell = "MyFirstCell"
}
}
override func viewDidLoad() {
super.viewDidLoad()
//HERE ADD THE FIRST ROW ?
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyFirstCell, forIndexPath: indexPath) as! MyFirstCell
cell.displayCell(firstData)
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell
cell.displayCell(data[indexPath.row])
return cell
}
}
If I understand your question properly this problem can be easily :)
You always wants to show a first cell in your tableView no matter whether there is a data in your data array or not :) and your problem is you cant add the first object to data array thats all right buddy :)
Here is a solution :)
Don't do anything in ViewDidLoad :) Simply save your first row data object in your local variable lets say : yourCustomObject
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count + 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : myTestCell = tableView.dequeueReusableCellWithIdentifier("testCell")! as! myTestCell
if indexPath.row == 0 {
cell.nameLabel.text = yourCustomObject.property
}
else {
cell.nameLabel.text = data[indexPath.row -1].property
}
return cell
}
Problem solved :) Happy coding :)
How It works :
Simple,
Assume your data array is empty :) then it will return count as 0 :) But you always want to show your first cell isn't it :) so add +1 to data array count :) return data.count + 1
Now handle it carefully in cellForRowAtIndexPath. You dont want to end up accessing data in data array for first object so check for indexpath 0.
And you dont want to end up accessing object out of index of data so use data[indexPath.row -1]
Hope I made my point clear :) Happy coding
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count + 1 //Add plus 1 for firstData
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell
if indexPath.row == 0 {//First index will be firstData
cell.displayCell(firstData)
} else { //All other cell's will be filled with data array
cell.displayCell(data[indexPath.row - 1]) //Make sure you offset indexPath.row by 1 so you start at index 0 of data array
}
return cell
}

TableView Controller -Swift

I'm trying to present an array of names into cells of tableview controller.
Here's the code:
import UIKit
class TableViewController: UITableViewController
{
var names = ["aA", "aB", "cC"]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return names.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
cell.detailTextLabel!.text = names[indexPath.row]
return cell
}
}
When I simulate it, nothing has appeared into the cells!
What's wrong with my Code!
The compiler did not give me any errors.
Update number of sections. Your problem will be solved.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

UITableView not showing in order SWIFT

having some issue with tableview. The JSon content is in the right order but when i update the table thats when the problem starts. This is how i update the table:
var cats = [Categories]()
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
cats.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "CategoryTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CategoryTableViewCell
print(cats[indexPath.row].categoryName)
cell.nameLabel.text = cats[indexPath.row].categoryName
cell.subNameLabel.text = cats[indexPath.row].appShortDesc
return cell
}
When i print the JSon it shows the correct order just the tableview keeps doing what it feels like.

Resources