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)
}
Related
This is my app:
When i press the Edit button (''Rediger''), i get the following:
So my question is: How do i make the round red circle and the disclosure indicator hidden while the tableview is being edited? Both the circle and the disclosure indicator are imageViews.
Here is the code for the custom cell (i edited out the unnecessary parts):
class KundeavisCell: UITableViewCell {
#IBOutlet weak var unreadImage: UIImageView!
#IBOutlet weak var disclosureIndicatorImage: UIImageView!
}
And the view controller with the table:
class KundeaviserVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var redigerOutlet: UIBarButtonItem!
var stores = ["Rema 1000", "Coop Obs", "Coop Extra"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "KundeavisCell", for: indexPath) as! KundeavisCell
// Here i edit the cell
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stores.count
}
#IBAction func tableViewEditingPressed(_ sender: Any) {
if tableView.isEditing {
tableView.setEditing(false, animated: true);
redigerOutlet.style = UIBarButtonItemStyle.plain;
redigerOutlet.title = "Rediger";
} else {
tableView.setEditing(true, animated: true);
redigerOutlet.title = "Ferdig";
redigerOutlet.style = UIBarButtonItemStyle.done;
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let itemToMove = stores[sourceIndexPath.row]
stores.remove(at: sourceIndexPath.row)
stores.insert(itemToMove, at: destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
}
I know i can indent the images to the left by setting constraints on them, but i want to hide them completely. I found a question about it: Link but unfortunately it was written 5 years ago and in Objective-C.
Any help will be highly appreciated, thanks!
That code you reference is still valid to now. You already have the Custom implementation of the UITableViewCell so now implement the SetEditing function and in the implementation hide/shows your images.
override func setEditing(_ editing: Bool, animated: Bool) {
unreadImage.isHidden = editing
disclosureIndicatorImage.isHidden = editing
}
That should work.
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
}
In my iOS app I have a UITableViewController whose rows contain data and data picker. I want to change te function of the buttons Edit and Done. I would like the Edit button to allow the user to write/insert data (instead of deleting rows), while I would like the Done button to save to save the data (and not just exiting the edit mode). I've added to my code the following:
// The following two functions remove the red minus sign
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
in order to avoid appearing the circle red minus, but how can I override the functions according to Edit or Done value of the button? I heard about delegates but I'm new to iOS and I don't know what they are...if someone can explain me I would be grateful.
Please try this code block :-)
import UIKit
// You can initialise an instance of this class manually or configure it on storyboard
class TableViewController: UITableViewController {
// MARK:- This part is for demo purpose
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.yellowColor()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
// MARK:- From here is the main part to answer your question
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit") { (action, indexPath) -> Void in
print("Write/Insert data here")
}
let doneAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done") { (action, indexPath) -> Void in
print("Save data here")
}
doneAction.backgroundColor = UIColor.blueColor()
return [doneAction, editAction]
}
}
You will see the result like this
I want a table view in my view to always be re-orderable. I cannot figure this out. I have rows in my table view but can't get the re-ordering handles to show up.
For the table view prototype cell, I made sure there was a check in Indentation > "Shows Re-order Controls", but this didn't do anything.
This is how my whole class looks like:
class ViewController: UIViewController {
...
#IBOutlet var currentTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
currentTable.editing = true
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return unitCategories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = "\(unitCategories[indexPath.row])"
return cell
}
/*func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}*/
}
Can someone help point me in the right direction?
You need to implement these two methods in order to be able to show the reordering control,
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
As stated in the header file,
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
Allows the reorder accessory view to optionally be shown for a
particular row. By default, the reorder control will be shown only if
the datasource implements:
-tableView:moveRowAtIndexPath:toIndexPath:
I am really new to the whole Swift thing and also programming in general. I have researched everywhere but couldn't find an answer for the error that I got. Please help me :)
import Foundation
import UIKit
class TümÜrünlerViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tümÜrünlerTableView: UITableView!
#IBAction func cancelTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
var ürünler = ["Havalı", "Daha Havalı", "Eheheh"]
var ürün = "Balık"
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.ürün = self.ürünler[indexPath.row]
self.performSegueWithIdentifier("başlıklardanÖzeleSegue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tümÜrünlerTableView.dataSource = self
self.tümÜrünlerTableView.delegate = self
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailViewController = segue.destinationViewController as BaşlıklarViewController
detailViewController.ürün = self.ürün
if self.ürün == "Havalı" {
detailViewController.label = "anan"
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
return self.ürünler.count
}
}
Here is my code but I am getting the error when I try to insert UITableViewDataSource. I tried everything that the forums said but it docent seem to work for me :(
My guess:
Let us look at this method. T needs to be decapitalized. tableView not TableView
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
It's a very simple mistake - the UITableViewDataSource requires that 2 methods are implemented:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
but you've just made a typo in the 2nd one:
func TableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
^
the method name should start in lowercase: tableView(...)
You have a capital T on your cellForRowAtIndexPath function. It should read:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell()
cell.textLabel!.text = self.ürünler[indexPath.row]
return cell
}
Reason behind error is that, you are not calling datasource required method in correct order. Correct order of calling is :
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Always, make sure you call numberOfRowsInSection before cellForRowAtIndexPath.