In my program, I am trying to implement the swipe to delete feature on UITableViewCells but the delete button is not appearing even though the cell does move as it should when swiping. Does anyone know why?
Here is my code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as resultsCell
otherName = cell.usernameLbl.text!
otherProfileName = cell.profileNameLbl.text!
self.performSegueWithIdentifier("goToConversationVC", sender: self)
}
override func viewWillAppear(animated: Bool) {
self.navigationItem.hidesBackButton = true
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return resultsUsernameArray.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 120
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:resultsCell = tableView.dequeueReusableCellWithIdentifier("Cell") as resultsCell
cell.usernameLbl.text = self.resultsUsernameArray[indexPath.row]
cell.profileNameLbl.text = self.resultsProfileNameArray[indexPath.row]
resultsImageFiles[indexPath.row].getDataInBackgroundWithBlock {
(imageData: NSData!, error:NSError!) -> Void in
if error == nil {
let image = UIImage(data: imageData)
cell.profileImg.image = image
}
}
return cell
}
#IBAction func logoutBtn_click(sender: AnyObject) {
PFUser.logOut()
self.navigationController?.popToRootViewControllerAnimated(true)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// called when a row is moved
func tableView(tableView: UITableView,
moveRowAtIndexPath sourceIndexPath: NSIndexPath,
toIndexPath destinationIndexPath: NSIndexPath) {
// remove the dragged row's model
let val = friends.removeAtIndex(sourceIndexPath.row)
// insert it into the new position
friends.insert(val, atIndex: destinationIndexPath.row)
}
func tableView(tableView: UITableView,
commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
switch editingStyle {
case .Delete:
// remove the deleted item from the model
friends.removeAtIndex(indexPath.row)
// remove the deleted item from the `UITableView`
resultsTable.editing = resultsTable.editing
resultsTable.editing = true
self.resultsTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
return
}
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return .Delete
}
Your UITableView is too big hence the delete button is out of the screen
Related
I have custom cells of UITableView which consist of UIImage and textLabel. When I select the editing mod, the textLabel shows the following image;
I want to move it to left. I tried all of things to do but it didn't work.
Please let me know.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allRows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "editCell", for: indexPath) as! CustomTableView
cell.textLabel?.text = self.allRows[indexPath.row].bname
cell.textLabel?.textAlignment = .left
let image:UIImage = UIImage(named: "group")!
cell.imageView!.image = image
return cell
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle{
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == .delete{
self.allRows.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)
}
}
#objc func leftEditButtonPressed(_ sender: UIButton) {
self.tableView!.isEditing = true
self.tableView?.setEditing(true, animated: true)
}
look at this, I hope it helps you:
swipe left/right in tableViewCell
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Closed")
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [closeAction])
}
Try using the following code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArr.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableCell") as? TestTableCell else {
return UITableViewCell()
}
cell.customImageView.image = #imageLiteral(resourceName: "ic_audio")
cell.customTitleLabel.text = self.dataArr[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("delete at \(indexPath.row)")
}
}
Here is the output for this code:
Note: You can change the editing action in editingStyleForRowAt method and for performing the action you need to write the logic in commit editingStyle method.
Hope this help you!
I suggest you to create new label in that UITableViewCell and connect that label to custom UITableViewCell class ! then you are free to do anything !
Swift 4 :
Create a new class which is subclass of UITableViewCell !
In your storyboard in right menu which called Utilities in toolbar of Utilities go to the Identity inspector defince your cell class to your new UITableViewCell class
Drag and drop UILabel from Object library into your cell
now using drag and drop connect that label to your UITableViewCell class then create an Outlet
Perfect !
now use myLabel instead of textLabel ;)
dont forget to cast your cell as! YourCustomCellClass
Good Luck ;)
I have an UITableView that I manage in a controller with UITableViewDelegate and UITableViewDataSource. In this table I have a custom cell, the problem is that the function editActionsForRowAtIndexPath gets called only sometimes (maybe when I swype in a particular way, I don't know), my code is the following:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let doneAction: UITableViewRowAction
//let highlightAction: UITableViewRowAction
if(self.compiti[indexPath.row].completato){
doneAction = UITableViewRowAction(style: .Normal, title: "Da Fare") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
let compito = self.compiti[indexPath.row]
self.db.contrassegnaCompito(compito)
UITableViewRowAction
}
doneAction.backgroundColor = UIColor.redColor()
}else{
doneAction = UITableViewRowAction(style: .Normal, title: "Fatto") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
let compito = self.compiti[indexPath.row]
self.db.contrassegnaCompito(compito)
}
doneAction.backgroundColor = UIColor(red: 67/255, green: 160/255, blue: 71/255, alpha: 0.7)
}
return [doneAction]
}
you need to add this method implementation as well or you won't be able to swipe to display the actions
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// it can be empty
}
It works for me with this code, try starting from this and you will probably find when the problem occurs
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
var doneAction :UITableViewRowAction!
doneAction = UITableViewRowAction(style: .Default, title: "Da Fare") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
UITableViewRowAction
}
return [doneAction]
}
I am making an tvOS app for the Apple TV but I have some problems with my UITableView.
When I click on a UITableViewCell nothing happens. I am using targetForAction but it doesn't seem to work.
This is my code:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
}
let allData = ["Los Angeles","New York","San Fransisco"]
#IBOutlet weak var tableView: UITableView!
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)
cell.textLabel?.text = "\(allData[indexPath.row])"
cell.targetForAction("buttonClicked:", withSender: self)
return cell
}
func buttonClicked(sender:AnyObject) {
print("button clicked!")
}
I found the solution.
I had to add this function:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("selected cell \(indexPath.row)")
}
Swift 5:
func tableView(_ tableView: UITableView, didSelectRowAtindexPath indexPath: IndexPath) {
print(indexPath.row)
}
Please use the UITableView Delegate method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
I have a swift app with a tableview where the cells are dynamic. What I want to achieve is that the user can side swipe a cell and it brings up two tiles, one for editing and one for deleting (an example of this would be in the messages app where you side swipe for the delete option)
I have got the two tiles to show by using:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit ", handler:{action, indexpath in
});
moreRowAction.backgroundColor = UIColor.orangeColor()
let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
return [editRowAction, deleteRowAction]
}
The first issue I have is how can I programatically close the side swipe when the edit option is selected so that the tiles are hidden and the user can see the textfield in the cell?
Second issue, when a cell is in edit mode I want to be able to move the cell in the tableview: following tutorials I have implemented the below:
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let sourceRow = sourceIndexPath.row;
let destRow = destinationIndexPath.row;
let object = ArrayList.objectAtIndex(sourceRow)
ArrayList.removeObjectAtIndex(sourceRow)
ArrayList.insertObject(object, atIndex: destRow)
}
and set the following when I want to put cell into moving mode
TableView.setEditing(true, animated: true)
Which sort of does what I want, however when I put it into editing mode I get the delete icon on the left side of the cell (red circle with white dash) which I don't want, ideally I'd like my own icon so user can select and drag cell around but I feel this might be pushing it slightly.
Thanks
To get rid of the remove button you should be able to set the editing style to none (or something else)
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return tableView.editing ? UITableViewCellEditingStyle.None : UITableViewCellEditingStyle.Delete
}
To hide to buttons after a swipe you can either set tableView.editing or reload the cell.
tableView.editing = false
alt
tableView.reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
//All the involved delegate methods:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit ", handler:{action, indexpath in
self.tableView.editing = false
})
let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
self.tableView.editing = false
})
return [editRowAction, deleteRowAction]
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return tableView.editing ? UITableViewCellEditingStyle.None : UITableViewCellEditingStyle.Delete
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
}
I do know that it's not too hard to do it in objective C , the problem is I'm learning Swift by skipping Objective C.
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html
However is there anything equivalent to the link above in Swift?
I have tried this...here is the code
In my example code there is button that starts the editing ---
Action Method of the button -->
#IBAction func editTableView (sender:UIBarButtonItem)
{
if listTableView.editing{
//listTableView.editing = false;
listTableView.setEditing(false, animated: false);
barButton.style = UIBarButtonItemStyle.Plain;
barButton.title = "Edit";
//listTableView.reloadData();
}
else{
//listTableView.editing = true;
listTableView.setEditing(true, animated: true);
barButton.title = "Done";
barButton.style = UIBarButtonItemStyle.Done;
//listTableView.reloadData();
}
}
And the related UITableView delegate methods -->
// The editing style for a row is the kind of button displayed to the left of the cell when in editing mode.
func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle
{
if (false == self.editing && !indexPath){
return UITableViewCellEditingStyle.None;
}
if (self.editing && indexPath.row == countryList.count){
return UITableViewCellEditingStyle.Insert;
}
else{
return UITableViewCellEditingStyle.Delete;
}
//return UITableViewCellEditingStyle.Delete;
}
// Update the data model according to edit actions delete or insert.
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)
{
if editingStyle == UITableViewCellEditingStyle.Delete{
countryList.removeAtIndex(indexPath.row);
self.editTableView(barButton);
listTableView.reloadData();
}
else if editingStyle == UITableViewCellEditingStyle.Insert{
countryList.append("New Country");
}
}
// Determine whether a given row is eligible for reordering or not.
func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool
{
return true;
}
// Process the row move. This means updating the data model to correct the item indices.
func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!)
{
let item : String = countryList[sourceIndexPath.row];
countryList.removeAtIndex(sourceIndexPath.row);
countryList.insert(item, atIndex: destinationIndexPath.row)
}
You can also download full code Here
All the same rules apply as in Objective-C. You set the table view data source and delegate just like you would in Objective-C.
func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true // Yes, the table view can be reordered
}
func tableView(tableView: UITableView!, moveRowAtIndexPath fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) {
// update the item in my data source by first removing at the from index, then inserting at the to index.
let item = items[fromIndexPath.row]
items.removeAtIndex(fromIndexPath.row)
items.insert(item, atIndex: toIndexPath.row)
}
If you need finer grain control, you can also implement
func tableView(tableView: UITableView!, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath!, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath!) -> NSIndexPath! {
…
}
Now there is a library for this reorder function: LPRTableView.
Converted Above Answer methods in Swift 3.0
// Determine whether a given row is eligible for reordering or not.
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// Process the row move. This means updating the data model to correct the item indices.
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item : Dictionary<String, Any> = arrInterval[sourceIndexPath.row]
arrInterval.remove(at: sourceIndexPath.row)
arrInterval.insert(item, at: destinationIndexPath.row)
}