How do you pass data from tableview cell button with a UILongPressGestureRecognizer? - swift

I have a button in my cell that if the user holds for a certain length of time it will trigger a popup. I am having trouble passing the cell data with the long press button.
Heres how I submit and pass data with a regular tap...
cell.addButton.tag = (indexPath as NSIndexPath).row
cell.addButton.addTarget(self, action: #selector(Dumps.addAction(_:)), for: UIControl.Event.touchUpInside)
.
#IBAction func addAction(_ sender: Any) {
let tag = (sender as AnyObject).tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag!, section: 0)) as! DumpsCell01
codeData = cell.codeField.text! }
The above works fine.
Heres how I submit the button with the long press gesture. Its passing nil through _sender I think
cell.deleteButton.tag = (indexPath as NSIndexPath).row
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(Dumps.deleteAction(_:)))
cell.deleteButton.addGestureRecognizer(longGesture)
.
#objc func deleteAction(_ sender: UIGestureRecognizer){
let tag = (sender as AnyObject).tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag!, section: 0)) as! DumpsCell01
cell.codeLabel.backgroundColor = UIColor.red }
How would I pass the data through this method?

You should be using the tag of the UIButton instead of the UILongPressGestureRecognizer as you have done above.
func deleteAction(_ sender: UILongPressGestureRecognizer) {
guard let tag = (sender.view as? UIButton)?.tag else { return }
let cell = tableView.cellForRow(at: IndexPath(row: tag, section: 0)) as? DumpsCell01
cell?.codeLabel.backgroundColor = .red
}
Note: I've also avoided force unwrapping as you should too through-out the project.

Related

How to fix indexPath error in viewDIdLoad?

I have problem with indexPath.row. Inside the cellForRowAt this lines work perfect:
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuViewCell", for: indexPath) as! MenuViewCell
let item = converters[indexPath.row]
cell.ConverterName.text = item.converterName
cell.converterImage.image = item.converterImg
But when i try to implement it inside viewdidload i've got an error:
"Use of unresolved identifier 'indexPath'"
And code:
struct converter {
let converterName: String
let converterImg: UIImage
}
var converters = [converter(converterName: "Converter of time", converterImg: #imageLiteral(resourceName: "time")),
converter(converterName: "Converter of pressure", converterImg: #imageLiteral(resourceName: "davlenie")),
converter(converterName: "Converter of speed", converterImg: #imageLiteral(resourceName: "skorost")),
converter(converterName: "Converter of distance", converterImg: #imageLiteral(resourceName: "dlinna"))]
override func viewDidLoad() {
super.viewDidLoad()
let item = converters[indexPath.row]
let itemSort = item.converterName
self.converters = itemSort.sorted { $0 < $1 }
self.tableView.reloadData()
}
viewDidLoad is not passed an IndexPath parameter like cellForRowAt is.
IndexPath is used to identify either the current cell to be built or selected. You do not need it in viewDidLoad. You have 4 converters in the array and can select any you like whenever you need to just by using its index.

Handling Previous, Next and Done buttons for keyboard across UITableView custom cells

First of all, I'm a fairly new to Swift and I've been looking for a good solution to handle Previous, Next and Done buttons on a keyboard across different custom cells in UITableView. I've looked at the various solutions on Stack Overflow but none of them fit 100% for what I need.
My tableview has one field (UITextField, UITextView, etc.) per row and need a generic way to move from one cell to the next. Some of the solutions don't account for scenarios where the next cell might be offscreen.
I've come up with a solution which I'll post as an answer. Feel free to comment on ways to improve if you have suggestions!
Please check this library. Simple and effective. You just to need to install via cocoa pods and single line code in appDelegate
pod 'IQKeyboardManagerSwift'
https://github.com/hackiftekhar/IQKeyboardManager
In App delegate
IQKeyboardManager.sharedManager().enable = true
For my custom cells, I have a base class as a foundation since I'm creating everything programmatically. It looks like this:
class BaseTableViewCell: UITableViewCell {
weak var delegate: BaseTableViewCellDelegate? = nil
var indexPath: IndexPath? = nil
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
fatalError("BaseTableViewCell setupViews not overridden")
}
func handleBecomeFirstResponser() {
// handle in derived class
}
func handleResignFirstResponder() {
// handle in derived class
}
}
Also, I've got a delegate for this base class that looks like this:
protocol BaseTableViewCellDelegate: class {
func cellEdited(indexPath: IndexPath)
func cellPreviousPressed(indexPath: IndexPath)
func cellNextPressed(indexPath: IndexPath)
func cellNeedsResize(indexPath: IndexPath)
}
// Using extension to provide default implementation for previous/next actions
// This makes then optional for a cell that doesn't need them
extension BaseTableViewCellDelegate {
func cellPreviousPressed(indexPath: IndexPath) {}
func cellNextPressed(indexPath: IndexPath) {}
func cellNeedsResize(indexPath: IndexPath) {}
}
I'm using an extension for a pure-swift mechanism to make the previous and next implementations optional in case we have a situation where we don't need these buttons.
Then, in my BaseTableViewCell class, I've got a function to setup the keyboard toolbar like this (below). I've got another function to support a UITextView as well (there might be a better way to do this; not sure).
func setupKeyboardToolbar(targetTextField: UITextField, dismissable: Bool, previousAction: Bool, nextAction: Bool) {
let toolbar: UIToolbar = UIToolbar()
toolbar.sizeToFit()
var items = [UIBarButtonItem]()
let previousButton = UIBarButtonItem(image: UIImage(imageLiteralResourceName: "previousArrowIcon"), style: .plain, target: nil, action: nil)
previousButton.width = 30
if !previousAction {
previousButton.isEnabled = false
} else {
previousButton.target = self
previousButton.action = #selector(toolbarPreviousPressed)
}
let nextButton = UIBarButtonItem(image: UIImage(imageLiteralResourceName: "nextArrowIcon"), style: .plain, target: nil, action: nil)
nextButton.width = 30
if !nextAction {
nextButton.isEnabled = false
} else {
nextButton.target = self
nextButton.action = #selector(toolbarNextPressed)
}
items.append(contentsOf: [previousButton, nextButton])
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
items.append(contentsOf: [spacer, doneButton])
toolbar.setItems(items, animated: false)
targetTextField.inputAccessoryView = toolbar
}
Here are the associated action routines:
func toolbarPreviousPressed() {
if delegate != nil && indexPath != nil {
delegate?.cellPreviousPressed(indexPath: indexPath!)
}
}
func toolbarNextPressed() {
if delegate != nil && indexPath != nil {
delegate?.cellNextPressed(indexPath: indexPath!)
}
}
In my view controller where I have the tableview, my cellForRowAt function has this code:
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell") as! TextFieldCell
let addressItem = (item as! XXXXXXAddressViewModelItem)
cell.textField.placeholder = addressItem.placeHolderText
cell.textField.text = addressItem.getValue(row: 0)
cell.indexPath = indexPath
cell.delegate = self
cell.setupKeyboardToolbar(targetTextField: cell.textField, dismissable: true, previousAction: false, nextAction: true)
return cell
Here is how I handle the delegate methods for the previous and next buttons being pressed:
func cellPreviousPressed(indexPath: IndexPath) {
// Resign the old cell
let oldCell = tableView.cellForRow(at: indexPath) as! BaseTableViewCell
oldCell.handleResignFirstResponder()
// Scroll to previous cell
let tempIndex = IndexPath(row: indexPath.row, section: indexPath.section - 1)
tableView.scrollToRow(at: tempIndex, at: .middle, animated: true)
// Become first responder
let cell = tableView.cellForRow(at: tempIndex) as! BaseTableViewCell
cell.handleBecomeFirstResponser()
}
func cellNextPressed(indexPath: IndexPath) {
// Resign the old cell
let oldCell = tableView.cellForRow(at: indexPath) as! BaseTableViewCell
oldCell.handleResignFirstResponder()
// Scroll to next cell
let tempIndex = IndexPath(row: indexPath.row, section: indexPath.section + 1)
self.tableView.scrollToRow(at: tempIndex, at: .middle, animated: true)
// Become first responder for new cell
let cell = self.tableView.cellForRow(at: tempIndex) as! BaseTableViewCell
cell.handleBecomeFirstResponser()
}
Finally, in my cell class derived from BaseTableViewCell, I override the handleBecomeFirstResponder and handleResignFirstResponder like so:
override func handleBecomeFirstResponder() {
textField.becomeFirstResponder()
}
override func handleResignFirstResponder() {
textField.resignFirstResponder()
}
On a related note, I handle the keyboard show and hide notifications by using insets on the tableview:
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, (keyboardFrame?.height)!, 0)
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, (keyboardFrame?.height)!, 0)
and:
self.tableView.contentInset = UIEdgeInsets.zero
self.tableView.scrollIndicatorInsets = UIEdgeInsets.zero
This took me a lot of trial-and-error to get this right and not seriously convolute my view controller with code that should be in the cell class.
I'm always looking for better ways to do this. Let me know your thoughts!

Segue to detailed view controller using a button in a cell

I have a collection view cell that passes data to a detailed view controller. When the cell is clicked, it segues into a view controller with more details. In the cells, I have a button, when the button is clicked, it also segues into a detailed view controller but a different view controller from when the cell is clicked.
This is what my didselect function looks like.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{
let vc = segue.destination as! BookDetailsViewController
let cell = sender as! UICollectionViewCell
let indexPath = self.CollectionView!.indexPath(for: cell)
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
let imagesTwo = post["imageTwo"] as? String
let imagesThree = post["imageThree"] as? String
let imagesFour = post["imageFour"] as? String
let imagesFive = post["imageFive"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames
vc.imagesTwo = imagesTwo
vc.imagesThree = imagesThree
vc.imagesFour = imagesFour
vc.imagesFive = imagesFive
print(indexPath?.row)
} }
if segue.identifier == "UsersProfile" {
if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{
let vc = segue.destination as! UsersProfileViewController
let cell = sender as! UICollectionViewCell
let indexPath = self.CollectionView!.indexPath(for: cell)
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
let username = post["username"] as? String
let userpicuid = post["uid"] as? String
vc.username = username
vc.userpicuid = userpicuid
print(indexPath?.row)
}}}
For if the segue == User's Profile I get an error in the let cell = line. My button in the cell was created in the cellForItemAt collection view function
let editButton = UIButton(frame: CGRect(x: 106, y: 171, width: 36, height: 36))
editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside)
editButton.tag = indexPath.row
print(indexPath.row)
editButton.isUserInteractionEnabled = true
cell.addSubview(editButton)
When I click the cell, it works perfectly and segues me into a detailed view controller but when I click the button within the cell, I get an error.
Here is my editTappedButton function
#IBAction func editButtonTapped() -> Void {
print("Hello Edit Button")
performSegue(withIdentifier: "UsersProfile", sender: self)
}
It is obvious that you are getting that crash because with your button action you are calling performSegue(withIdentifier: "UsersProfile", sender: self) now with sender you are passing self means reference of current controller not the UICollectionViewCell what you need is get the indexPath of that cell and pass that and now in prepareForSegue cast the sender to IndexPath instead of UICollectionViewCell.
First replace your editButtonTapped with below one
#IBAction func editButtonTapped(_ sender: UIButton) -> Void {
print("Hello Edit Button")
let point = sender.superview?.convert(sender.center, to: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: point!) {
performSegue(withIdentifier: "UsersProfile", sender: indexPath)
}
}
Now in prepareForSegue for identifier UsersProfile cast the sender to IndexPath or simply replace your condition with my one.
if segue.identifier == "UsersProfile" {
if let indexPath = sender as? IndexPath{
let vc = segue.destination as! UsersProfileViewController
let post = self.posts[indexPath.row] as! [String: AnyObject]
let username = post["username"] as? String
let userpicuid = post["uid"] as? String
vc.username = username
vc.userpicuid = userpicuid
print(indexPath.row)
}
}

2 items in a cell with same tapGestureRecognizer (swift)

in my app i have a tableview with a profile image and a username label. If you click on one of the 2 then the need to do this function:
func goToProfileScreen(gesture: UITapGestureRecognizer) {
self.performSegueWithIdentifier("profile", sender: nil)
}
however if i try to implement this in my cellForRowAtIndexPath it only works for the last time i've added it.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell") as? NewsCell {
let post = self.posts[indexPath.row]
cell.request?.cancel()
let profileTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(NewsVC.goToProfileScreen(_:)))
profileTapRecognizer.numberOfTapsRequired = 1
// profileTapRecognizer.delegate = self
cell.profileImg.tag = indexPath.row
cell.profileImg.userInteractionEnabled = true
cell.profileImg.addGestureRecognizer(profileTapRecognizer)
cell.usernameLabel.tag = indexPath.row
cell.usernameLabel.userInteractionEnabled = true
cell.usernameLabel.addGestureRecognizer(profileTapRecognizer)
var img: UIImage?
if let url = post.profileImageURL {
if url != "" {
img = NewsVC.imageCache.objectForKey(url) as? UIImage
}
}
cell.configureCell(post, img: img)
cell.selectionStyle = .None
return cell
} else {
return NewsCell()
}
}
so now it works for the username label. if i put the usernamelabel first in the code and then the profileImg, then it only works for the profileImg?
how can i get it to work for both of them?
You need to use 2 different tapRecognizers, because a UITapGestureRecognizer can only be attached to one view.
("They are objects that you attach to a view", Apple Doku)
let profileTapRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(NewsVC.goToProfileScreen(_:)))
let profileTapRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(NewsVC.goToProfileScreen(_:)))
profileTapRecognizer1.numberOfTapsRequired = 1
profileTapRecognizer2.numberOfTapsRequired = 1
// profileTapRecognizer1.delegate = self
// profileTapRecognizer2.delegate = self
cell.profileImg.tag = indexPath.row
cell.profileImg.userInteractionEnabled = true
cell.profileImg.addGestureRecognizer(profileTapRecognizer1)
cell.usernameLabel.tag = indexPath.row
cell.usernameLabel.userInteractionEnabled = true
cell.usernameLabel.addGestureRecognizer(profileTapRecognizer2)

checkbox button in tableview in Swift

I'm trying to create a checkbox functionality in a tableview .
It's a to-do list where if the checkbox is pressed the cell hides and appears as completed in a different section.
Coming from C# this was very straightforward to do but in swift there isn't even a checkbox button to start with :(...
I made it to work by adding a button with two images(checked, unchecked) to a custom prototype cell in IB.
Since you can't have the tableView and the in-cell-button declared in the same viewcontroller/class I had to subclass the tableViewCell.
Now, how do I access the checkbox from didSelectRowAtIndexPath ? When I select the cell the event fires but when I press the checkbox button in the same cell nothing fires and I can't hide the cell.
var indexTag = checkBoxImage.tag
//this is what I have in TableViewCell class
#IBAction func checkBoxInCell(sender: UIButton) {
checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
if isChecked != false {
isChecked = false
cellitemcontent.removeAtIndex(indexTag)
//can't access the cell from here to update the tableview
}
else {
isChecked = true
checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
}
}
//this is what I have in my FirstViewController that contains the tableview
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
cellitemcontent.removeAtIndex(indexTag)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
//when I press the checkBoxImage button in the cell it doesn't fire this event...
}
I use sample code below to implement check box button in cell (Xcode 7/ Swift 2.0):
-In viewDidLoad {}:(save check box state in each cell to .plist file)
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
//if (let a != (fM.fileExistsAtPath(fnstring))) {
if !(fM.fileExistsAtPath(fnstring)) {
let onlyarr = NSMutableArray()
for var i = 0 ; i < 5; ++i{ // number of sections
var arr = NSArray()
switch i { // number cell in each section
case 0: arr = [Int](count: 12, repeatedValue: 0) // 0 means unchecked box.
case 1: arr = [Int](count: 13, repeatedValue: 0)
case 2: arr = [Int](count: 14, repeatedValue: 0)
case 3: arr = [Int](count: 15, repeatedValue: 0)
case 4: arr = [Int](count: 16, repeatedValue: 0)
default: arr = [Int]()
}
onlyarr.addObject(arr)
}
onlyarr.writeToFile(fnstring, atomically: false)
}
- In override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { }
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
if fM.fileExistsAtPath(fnstring) {
let chcklstDick = NSMutableArray(contentsOfURL: NSURL(fileURLWithPath: fnstring))
let chcklstsortedCat: NSArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
var img = UIImage()
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
NSLog("j: %i", j)
if j > 0 {
img = UIImage(named: "checked")!
}else {
img = UIImage(named: "unchecked")!
}
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, img.size.width, img.size.height)
bttn.setBackgroundImage(img, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
}
}else {NSLog("nonononononononno")}
- In func chckBttnTapped(sender: AnyObject, eventy event: AnyObject) { }
let touches: NSSet = event.allTouches()!
let touch: UITouch = touches.anyObject()! as! UITouch
let crrntTouchPos : CGPoint = touch.locationInView(self.tableView)
let idxpth: NSIndexPath = self.tableView.indexPathForRowAtPoint(crrntTouchPos)!
if idxpth.row != NSNotFound {
self.tableView(self.tableView, accessoryButtonTappedForRowWithIndexPath: idxpth)
}
-In override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { }
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
if fM.fileExistsAtPath(fnstring) {
let chcklstDick = NSMutableArray(contentsOfURL: NSURL(fileURLWithPath: fnstring))
let chcklstsortedCat: NSMutableArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
var newimg = UIImage()
if j == 0 {
newimg = UIImage(named: "checked")!
}else {
newimg = UIImage(named: "unchecked")!
}
let cell = tableView.dequeueReusableCellWithIdentifier("cellID", forIndexPath: indexPath)
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, newimg.size.width, newimg.size.height)
bttn.setBackgroundImage(newimg, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
self.tableView.reloadData()
self.tableView.reloadInputViews()
ttt.replaceObjectAtIndex(indexPath.row, withObject: 1 - j)
chcklstsortedCat?.replaceObjectAtIndex(indexPath.section, withObject: ttt)
chcklstsortedCat?.writeToFile(fnstring, atomically: false)
}
}
Hope its useful.
I know what you are trying to accomplish, but I would tackle it a different way. Perhaps in your model, you could have an array of tasks that are pending, and another array of tasks that are completed.
The number of sections can be 2. The number of rows in sections 0 and 1 can be the number of elements in the first and second arrays respectively.
When didSelectRowAtIndexPath is called, you can remove the item at that index in the first array and add the task to the second array. Then you must call tableView.reloadData().
I know you want to just pick up the row and change the placement of it, but in iOS the cells get reused. It's best to update the data source and then reload the table.
For the checkmarks, you can ensure that the items in Section 0 do not have the checkmark accessory, while the items in Section 1 do. You would set the accessory in cellForRowAtIndexPath after the cell has been dequeued.