I'm having this strange issue in my app where it sometimes takes several seconds (10ish) for a custom TableViewCell to trigger a segue to another ViewController. If I click anywhere else in the app during this time, the click triggers the segue.
The problem only occurs when selecting the same cell several times. Meaning if you click the first cell, segue over to the new ViewController, exit the new ViewController and click the same cell again, the problem could occur.
I've reconstructed this issue in a small project here for any kind souls to try out.
http://www.filedropper.com/customcellsegue_2
Any ideas guys?
Edit
Alright here's some code for you guys who don't wanna download the app.
Original ViewController
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
// Number of cells in each section.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
// Fill UITableView...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Selected")
}
Custom Table Cell
class customCell: UITableViewCell {
#IBOutlet var firstLabel: UILabel!
#IBOutlet var secondLabel: UILabel!
}
New ViewController
#IBAction func close(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
I've simply applied a modal segue from the custom cell to the new ViewController.
Related
I have strange behavior with UITableView on Storyboard today. I have created UITableView on Storyboard. After that I drag a PrototyleCell to this table and choose style is Basic. And I implement DataSource and Delegate on my ViewController. It show to simulator normal. But I can't tap to table for select a cell and didSelectCellAtIndexPath don't work too. In Storyboard I have checked selectionStyle. If I change style to another style, It work normally.
So my question is: this is a bug or it is a behavior of UITableView? And anyone can give some explanation for it.
Here is my code: Problem Cell Code. I can't select when use it but when I set another style of cell on storyboard everything will ok.
Thanks in advance
No its neither bug nor normal behavior, you must be doing some basic mistake...
Try following code with tableview and prototype cell in storyboard, Everthing will work.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var dataSource = ["Ajay","Ajay","Ajay","Ajay","Ajay","Ajay"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "cell")
}
cell!.textLabel?.text = dataSource[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
} }
Have you added the
tableView: cellForRowAtIndexPath delegate method. This is a required delegate you must implement.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
checkRequirements() { () -> Void in
var room = self.rooms[indexPath.row]
self.roomForPreview = room
self.indexPathForPreview = indexPath
self.performSegueWithIdentifier(CONSTANTS.SegueLobbyToRoomPreview, sender: self.view)
}
}
I perform a segue when a user selects a cell, but when I unwind, I find that the cell still has a grey background.
How can I make the cell grey only on touch? Once the touch is gone, the cell goes back to normal color.
Use deselectRowAtIndexPath:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
...
If you're using a UITableViewController, you can set the clearsSelectionOnViewWillAppear property to true - this will work as well.
How can I make a static table view to create an action when one of the cells is clicked in Swift?
I have created a static table like a general menu of the app, I can directly create a push segue when one of the cells are clicked. But at the same time when I click to one of the seques, I want the below function to be run. By draging a cell to the UITableView in storyboard the create action option is not appearing.
var goToProfiles = PFObject(className: "goToProfile")
goToProfiles["user"] = PFUser.currentUser()!.username
goToProfiles["goToUser"] = usernameLbl.text
goToProfiles.save()
If you use sections you will also need to query them.
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.section)
print(indexPath.row)
if indexPath.section == 1 && indexPath.row == 1 {
// do something
}
}
I found the solution with the code below:
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 1 {
//here you can enter the action you want to start when cell 1 is clicked
}
}
For swift 3 compatibility:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Your action here
}
I have a custom UITableViewCell containing a UIActivityIndicatorView (spinner), and I try to click on the cell so that spinner starting to animate. So I try to implement following in UITableViewController:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("testcase", forIndexPath: indexPath) as TestCaseTableViewCell
cell.spinner.startAnimating()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
I have the instance variable "spinner" in my TestCaseTableViewCell(custom cell class):
#IBOutlet weak var spinner: UIActivityIndicatorView!
But it didn't work......
I just want to click on the cell, and the spinner starts to animate cause I want to do something in this period. While the something is done, I can show something like "OK" in the cell(as the same position of the spinner). How can I achieve that?
The problem is with how you are retrieving your cell from the table view: dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath). This method asks the UITableView for a cell from its reuse cache when you need a new cell to display, so should only be used in the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) method of your table view's data source.
To ask the table view for an on-screen cell, use cellForRowAtIndexPath(indexPath: NSIndexPath). Your code sample then becomes:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? TestCaseTableViewCell {
cell.spinner.startAnimating()
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Another simple method:
Select your UIActivityIndicatorView and check "Animating" in the attribute inspector
Check "hidden"
Now do this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("testcase", forIndexPath: indexPath) as TestCaseTableViewCell
cell.spinner.hidden = false // <== Here
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Don't forget to hide the unhidden UIActivityIndicatorView if needed ;)
I'm creating a UiTableView with 2 different cell nibs. it is working perfectly fine expect 1 specific scenario.
ISSUE:
On cell selection, i perform some action in didSelectRowAtIndexPath. After didSelectRowAtIndexPath execution, iOS invokes cellForRowAtIndexPath method and recreates that clicked cell.
What is the affects of recreation:
My cells heights are dynamic. When UiTableView recreates any cell from middle, it causes unnatural jerks on scrolling back/up.
Below is the code snippet of my methods. Any help would be appriciated :)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var post = self.posts[indexPath.row]
if(post.valueForKey(MEDIA) != nil) {
var cell:PostImageTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("PicCell") as PostImageTableViewCell
cell.loadPostData(post)
return cell
} else {
var cell:PostTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as PostTableViewCell
cell.loadPostData(post)
return cell
}
}
Selection method:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedRow = indexPath.row
self.performSegueWithIdentifier("detailSegue", sender: nil);
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Call Stack: