this is my code:
class PlayersNamesTableViewController: UITableViewController {
var players = [Player]()
let numberOfPlayers = 30
override func viewDidLoad() {
super.viewDidLoad()
loadPlayers()
}
func loadPlayers(){
for index in 0..<numberOfPlayers{
let player = Player(name: "Player \(index)")
players += [player]
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("playerCellIdentifier", forIndexPath: indexPath) as UITableViewCell
let player = players[indexPath.row]
cell.textLabel?.text = "\(player.name)"
return cell
}
}
in my storyboard, i select Basic for the style of the cell.
My problem is that the table view is not scrollable, i just can see the first 14 players, like this:
Update 1
I put print("\(indexPath.row)") on the override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { method and I can see that there are just 15 values being printed, where are the other 30?
Update2
Scroll is enabled on the table view
Update3
User Interaction is enabled, as u see here
Here is your answer. Check your mouse. In simulator you should click to screen and scroll.
Related
I have a view controller with a tableview inside that is grouped. This produces a very weird glitch that causes the sectionTitles to show at the side. I've attached top, trailing, bottom, and leading constraints to the superview for the tableview and I've tested returning UITableViewCell() in cellForRowAtIndexPath to see if it was the cell that was causing the problem, but it still shows it like this.
var sectionTitles = ["Customer Orders", "Unprocessed Orders", "Processed Orders"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform segue to order list in future
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 117.0
}
After hours I figured out that I didn't implement this method
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
If you have only one section you don't put many titles.
If instead you want to give a title for each cell you have to add a label in your customized tableViewCell and in your cellForRowAtIndexPath you have to put something like this (after you have created a NSObject class):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
let order = Order[indexPath.row]
cell.yourTitleLabel.text = order.title
return cell
}
I have a UITableView inside a UIViewController. I am trying to make it so when the user click the BarButtonItem in the NavigationBar, that the tableview goes into editing mode, and thus the user can drag and reorder the cells.
However, what happens is that I simply press the editing button, and nothing happens.
This is what I have tried:
For the array declaration:
var tester = ["1", "2", "3"]
For the button declaration:
#IBAction func editButtonPressed(sender: AnyObject) {
self.editing = !self.editing
}
For the various tableview functions:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return tester.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("editCell", forIndexPath: indexPath) as! EditTableViewCell
cell.nameHolder?.text = tester[indexPath.row]
// Configure the cell...
return cell
}
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
var itemToMove = tester[fromIndexPath.row]
tester.removeAtIndex(fromIndexPath.row)
tester.insert(itemToMove, atIndex: toIndexPath.row)
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
What could be causing this bug?
Swift 4.1
In → func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
Add this line to configure the cell:
cell.isEditing = self.tableView(tableView, canMoveRowAt: indexPath)
Ran Hassid has the most correct answer to this in his comment.
This will solve the problem:
#IBOutlet weak var tableView: UITableView!
#IBAction func editButtonPressed(sender: AnyObject) {
tableView.setEditing(!tableView.isEditing, animated: true)
}
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.
I set hidesBarsOnSwipe=true in two view controllers, the Foo view controller which does not have a toolbar pushes Bar view controller which does have a toolbar. When Bar pop back to Foo, the toolbar will show when I scroll the Foo view controller. If I don't set self.navigationController?.toolbarHidden = false in Bar view controller, the toolbar will not show in neither Bar nor Foo view controller. I work with iOS8 SDK + Xcode 7.1.1 + Swift 2.1.
class FooTableViewController : UITableViewController
{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.hidesBarsOnSwipe = false
self.navigationController?.toolbarHidden = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.toolbarHidden = true
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "12345678"
return cell
} }
class BarTableViewController : UITableViewController
{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.hidesBarsOnSwipe = true
self.navigationController?.toolbarHidden = false
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.hidesBottomBarWhenPushed = false
self.navigationController?.toolbarHidden = true
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "12345678"
return cell
}}
You are up against 2 problems:
You cannot add anything to a UITableViewController
All the programmatic toolbarHidden and hidesBarsOnSwipe is only confusing the OS,and you will end up with toolbars on the wrong layer, scrolling along with the table view, and similar oddities with the navigation bar.
What you want is for the second table view to be handled by a UIViewController, not a UITableViewController. There is little programmatic change, you only need your UIViewController to adopt the data source and table delegate protocols.
No need to tinker with showing/hiding the toolbar. The code is much simpler, you can define everything not listed below in the Storyboard.
Foo (no toolbar):
class FooTableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Foo \(indexPath.row)"
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("Bar", sender: self)
}
}
Bar (with toolbar):
class BarTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Bar \(indexPath.row)"
return cell
}
}
Final Product:
► Find this solution on GitHub and additional details on Swift Recipes.
I have a tableviewcontoller, it retrieves the data and stores in model objects. The model object is assigned to table cells. when i make a change in table cell text box, the changes are not get updated to model. Is anything wrong in this code? if this is not right approach, please let know how to get the changes in table row.
class myTable: UITableViewController , UITableViewDelegate, UITableViewDataSource{
var models = [Customer]()
override func viewDidLoad() {
super.viewDidLoad()
self.generateUpdateModels()
self.tableView.dataSource = self
self.tableView.delegate = self
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("rowIdentifier", forIndexPath: indexPath) as! TextInputLabelTableViewCell
cell.first.text = models[indexPath.row].first //label
cell.last.text = models[indexPath.row].last //label
cell.status.text = models[indexPath.row].status //textbox
return cell
}
func generateUpdateModels(){
//update models[]
}
#IBAction func done(sender: AnyObject) {
//look into models for changes....
// don't see the changes
self.performSegueWithIdentifier("final", sender: self)
}