I am using the following method to move between viewControllers:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "segueJokeDetail", sender: self)
}
That's the capture from the storyboard showing segueId
If you do a segue from a UITableViewCell in interface builder, you don’t need to do it in the UITableView delegate as well.
Related
I would like to know if it was possible to use UITableViewVibrantCell on a UITableView (not controller). I can't figure out how to do it.
I'm doing :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView(tableView, cellForRowAt: indexPath) as! UITableViewVibrantCell
cell.blurEffectStyle = SideMenuManager.default.menuBlurEffectStyle
return cell
}
My UIViewController inherit from UITableViewDataSource and UITableViewDelegate.
Thank you for your help :)
Since I've upgraded to Xcode 9 / Swift 4 some of my UITableViewDelegate methods are not called anymore
As of Xcode 9 / Swift 4 all Objective-C methods should be marked #objc. The compiler does a reasonable job of recognizing where it should be applied however it doesn't figure out inheritance. For example:
class Delegate: NSObject, UITableViewDelegate {
}
class SuperDelegate: Delegate {
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}
This will not generate any warning, crashing or build error. However your line will not be called until you add #objc:
#objc class SuperDelegate: Delegate {
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}
I would like to update the content of my custom cell with his height so I wrote this code :
extension MyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
in my customCellTableView xib i added the top,right,left,bottom constraint to all the labels and also the numberOflines properties to 0
but when t run the app i got a cell of 250 height to all the cells which is very abnormal. I work on Xcode 9 and swift 4.
please if someone can help me,
I am trying to make my tableview cell from viewcontroller 1 change when the user click on a cell from tableview cell from viewcontroller 2. I know that I have to work with the function
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
however, I am not sure how I would accomplish what I wanted.
Here's my code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = changeHotelTableView.indexPathForSelectedRow;
let currentCell = changeHotelTableView.cellForRow(at: indexPath!) as! ChangeHotelTableViewCell
var valueToPass = currentCell.textLabel?.text
}
I want to get the valueToPass variable into my cell at another' view controller; say this is called timeViewController with timeTableView and a timeTableViewCell. How will I do this?
Thanks for your help and time in advance
Perhaps you should try with Protocol.
ViewController2:
protocol UserPressTheCell: NSObjectProtocol
{
func didUserPressTheCellWith(indexPath: IndexPath, text: String)
}
class ViewController2: UIViewController
{
//declare delegate
weak var delegate: UserPressTheCell?
....
func tableview(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath)
{
let indexPath = changeHotelTableView.indexPathForSelectedRow;
let currentCell = changeHotelTableView.cellForRow(at: indexPath!) as! ChangeHotelTableViewCell
self.delegate?.didUserPressTheCellWith(indexPath: indexPath, text: currentCell.textLabel?.text)
}
}
Then implement in ViewController1:
class ViewController1: UIViewController, UserPressTheCell
{
#IBOutlet weak var tableview: UITableView!
...
//MARK: UserPressTheCell
func didUserPressTheCellWith(indexPath: IndexPath, text: String)
{
let cell = self.tableView.cellForRow(at: indexPath!) as! "Your VC1's cell"
cell.textLabel?.text = text
}
}
To be sure delegate will not be nil:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let vc2 = segue.destination as! ViewController2
vc2.delegate = self
}
or wherever you segue to ViewController2.
Maybe this is the best way to accomplish what you wanted.
I have an iOS project I'm working on using Xcode7 and Swift2. I have a TableView that fetches an array from NSCoding. I have it started so the user can reorder the TableViewCells in the TableView. However I need it to save the new order once the user is finished and clicks 'done' which is a UIBarButtonItem. I have a value in my NSCoding object called cellOrder. I looked here and saw this for CoreData. How how would I do this for NSCoding and where do I save it?
I have the following code started for the TableViewCell movement:
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true}
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let itemToMove = details[fromIndexPath.row]
details.removeAtIndex(fromIndexPath.row)
details.insert(itemToMove, atIndex: toIndexPath.row)
}
details is the array my data is kept in, using NSCoding.
I decided the best way for me was to save the details array to NSUserDefaults after the the reorder was done. I saw here how to convert the NSObject to NSData to be saved. My code for the moveRowAtIndexPath section is now:
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let itemToMove = details[fromIndexPath.row]
details.removeAtIndex(fromIndexPath.row)
details.insert(itemToMove, atIndex: toIndexPath.row)
// Archive NSObject and save as NSData
let myData = NSKeyedArchiver.archivedDataWithRootObject(details)
// NSUserDefaults Save for Reorder of cells
reportDefaults.setObject(myData, forKey: "savedReportListKEY")
reportDefaults.synchronize()
// NSCoding Save
saveReport()
}