This is a weird question and counter intuitive to how tableviews work, however I have an array that creates 3 cells in the tableview. Since we are currently working on a beta and we only need the user to segue when they click on the first cell the 2 other cells DONT need to be used.
Here is some code of our tableview
import UIKit
class TableViewController: UITableViewController {
let locationManager = CLLocationManager()
//Store Array of Images
var imageArray = ["riverparkplace","mayfair","jamesonhouse",]
//Array of Image Names
var textArray = ["River Park Place", "MayFair", "Jameson House"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.requestAlwaysAuthorization()
if KCSUser.activeUser() == nil {
print("User Not Logged In")
performSegueWithIdentifier("jobsiteTOLogin", sender: nil)
} else {
//user is logged in and will be loaded on first call to Kinvey
var currentusername = KCSUser.activeUser().givenName
print("User named:\(currentusername) has logged in ")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
//cell.textLabel?.text = textArray[indexPath.row]
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = UIImage(named: imageArray[indexPath.row])
let textLabel2 = cell.viewWithTag(2) as! UILabel
textLabel2.text = textArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageArray.count
}
//On Click
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
//Buttons
@IBAction func logoutButton(sender: AnyObject) {
if KCSUser.activeUser() == nil{
//User is not logged in
print("Cant log out user since they are not logged in!!")
}else{
KCSUser.activeUser().logout()
print("User Logged out")
performSegueWithIdentifier("jobsiteTOLogin", sender: nil)
}
}
}
As you can see I only want the view to segue when you click on "riverrockplace" if you click on anything else it is ok if the app does nothing but would be even better if it returned a pop up notification.
Also at this stage when I segue I don't need to take data with me.
There are two ways you can approach this issue, using different types of segues:
shouldPerformSegueWithIdentifier:
and, using the sender
figure out which cell is calling it (as that will be sender
), and fire or not based on that information.TableViewController
to the destination. Then, in didSelectRowAtIndexPath
, determine if you should segue. If you should, then call performSegueWithIdentifier:
.Implement the didSelect Delegate method like this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0{
// perform segue
}else{
// no action needed
}
}
You can assign userInteractionEnabled = false to the cells that you won't be using to prevent the click animation from happening, as it might be confusing to your users if they click on the cell, its styling changes and nothing happens.
Alternatively, if you want to pop up an alert to provide some useful information, you can use the didSelectRowAtIndexPath method from your code to filter the index of the selction using indexpath.row, as suggested by Md.Muzahidul
If i understood your problem correctly, what you have to do is you want to navigate to XViewController, when you click on first cell of the tableView only.
In that case you have to use conditional segues, which you can create by dragging segue by clicking on the FilesOwner and not the cell itself as shown in the image below,
Then select the segue you have just created and set its identifier in the attribute inspector as shown in below image,
Next, you need to fire the segue in the didSelectRowAtIndexPath, change your code as shown below,
//On Click
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("ConditionalNavigationToXViewController", sender: self)
}
Then check the segue as below,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == "ConditionalNavigationToXViewController") {
segue.destinationViewController as? XViewController
}
}
Hope this would help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.