last day (14 days later) » 

2:52 PM
3
A: Getting indexPath from switch on UITableView

BanningsYou can use the indexPathForRowAtPoint like: @IBAction func switchChanged(sender: UIControl) { let rowPoint = sender.convertPoint(sender.frame.origin, toView: self.tableView) let indexPath = self.tableView.indexPathForRowAtPoint(rowPoint) println(indexPath) } This is the Objective ...

 
ACR
Since my tableView is in a different class than my custom cell's class, how do I deal with the toView: self.tableView in sender.convertPoint?
 
@ACR Do you want to get indexPath inside the cell?
 
ACR
I want to get the indexPath of the cell in the tableView. This is because in my app I have a tableView of 8 cells which corresponds to the 8 switches in the tableView.
 
@ACR I have updated my answer, hope this will help you
@ACR Let me know if that works.
 
ACR
@Bannings This returns nil every time I interact with the switch.
 
2:52 PM
@ACR I have fixed the issue for the loop
 
ACR
@Bannings It's still returning nil
 
Can you post your UI screenshot?
I have just updated twice
func tableView() -> UITableView? {
var tableView = self.superview
while (tableView != nil && !(tableView is UITableView)) {
tableView = tableView!.superview
}

return tableView as? UITableView
}
 
ACR
There's my UI
And the table I'm trying to deal with is the second one. I have settings in that table which I want to change the contents of the first one
 
Is the switch added by code? Can you post relevant code?
 
ACR
The switch was added in a .xib file where I made the cells. Here is the class that the .xib file is connected to
class SettingsCell: UITableViewCell {


@IBOutlet weak var settingsCellLabel: UILabel!
@IBOutlet weak var settingsCellSwitch: UISwitch!

let settingsView = SettingsViewController()

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

@IBAction func valueChanged(sender: AnyObject) {

let tableView = self.tableView()
 
3:06 PM
The self.tableView() is always returning nil?
 
ACR
The println(indexPath) is printing nil. I may have worded that wrong
 
The self.tableView() is nil or not? You can also print it
or can you post your project on anywhere?
 
ACR
self.tableView() returns this:
Optional(<UITableView: 0x7fd3f2891e00; frame = (0 127; 375 540); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fd3f1ccc110>; layer = <CALayer: 0x7fd3f1c52080>; contentOffset: {0, 0}; contentSize: {375, 400}>)
so I think that's working fine
 
The rowPoint is?
 
ACR
By printing out the rowPoint, my first cell prints
(622.0, 12.0)
 
3:17 PM
@IBAction func switchChanged(sender: UIControl) {
let tableView: UITableView? = self.tableView()
//let rowPoint = sender.convertPoint(CGPointZero, toView: tableView)
//let indexPath = tableView?.indexPathForRowAtPoint(rowPoint)
let indexPath = tableView?.indexPathForCell(self)
println(indexPath)
}
try this
 
ACR
That returns
Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})
 
Yes, it works
 
ACR
Yep, thank you!!
Can you just briefly explain what this does so I can do it in the future?
 
Actually I have never getting indexPath in custom cell
I think that the cell should just show anything
 
ACR
What do you mean?
 
3:25 PM
But should not handle logic
Do you know MVC pattern?
 
ACR
I've read about it a little
So is this a bad way of doing this?
 
Did you want to do anything with the SettingsViewController in the SettingsCell?
e.g. push or present
 
ACR
No, that line of code was in there because I was trying to figure out how to solve my problem. I'm going to delete that line
 
Why are you getting the indexPath?
 
ACR
So in my app, the settingsViewController is where the user decides which things they want to display in the MainTableViewController. My plan was by getting the indexPath of the switch, I can modify the array that is storing all the things they can choose to display. For example, if the user turns off the first cell, I could return indexPath of 0 so that my array can remove index 0. The array that I'm modifying would be used to populate the MainTableViewController
 
3:36 PM
The array is stored in the MainTableViewController, right?
 
ACR
Its global
 
OK, I think that's no problem if the array is global
It's pretty late by me. 23:40 pm
 
ACR
Ok, well thank you for all the help!
 
I'm sorry for my bad English...
 
ACR
It's no problem. I understood you
 

last day (14 days later) »