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 ;)
Related
I have a dark gray view background with a transparent tableview. I'm using the following code to try and stop cell highlight when a cell is clicked. It works except right when the cell is initially clicked, I see a highlight. I then transition to another scene after that. When I come back, the cell is not highlighted.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.clearColor()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
How do I disable the initial cell highlighting that is still going on?
Set UITableViewCell selection style none
cell.selectionStyle = .None
I found that the other answers did not work for me as they required a double click to select. This did work.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
UITableViewDelegate has methods to deal with cell highlights, probably
tableView(_:shouldHighlightRowAt:) is what you are looking for
Check the documentation for the other methods
The simplest way to prevent highlighting is setting selectionStyle to None. Here's how you can achieve this :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.selectionStyle = .None
return cell
}
How to disable highlight on tableview without disable the func? i tried these and now i can't go to the another page.
func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
Set the selectionStyle property of your UITableViewCell to .None.
This will permit selection but prevent the default highlighting behavior.
In the cellForRowAtIndexPath implementation in your controller, set the selectionStyle to .None as below:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.selectionStyle = .None
// ...
return cell
}
Step 1: Select your Tableview Cell
Step 2: After then go to Tableview Cell Properties and Set your
Tableview Cell Selection ".None"
Or you can also Do Programmatically add "Tablecell.selectionStyle = .none
" this code in your cellForRowAtIndexPath method.
I believe disabling selection should do the trick.
tableView.allowsSelection = false
Or, if you want the user to be able to temporarily tap the table you could do this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//your code
tableView.deselectRowAtIndexPath(indexPath)
}
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.
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.
I'm a new programmer and I'd like to start with a app where to show a table with an image and a label (dynamic). I can do it wist static table but now I'd like to know how to make it with swift. I'm following various tutorial but all say a easy table only array with text...
I'm sure your help will be useful many new as me...
Tutorial, code, are good accept...
import UIKit
class FirstViewController: UIViewController,UITableViewDelegate {
#IBOutlet weak var smilefoto: UIImageView!
var cellContent = ["Rob", "Kirsten", "Tommy", "Ralphie"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "tabella")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
}
What you'll have to do is create a custom UITableViewCell class, where you can create an imageview and label and anything else.
You should check this tutorial out:
https://www.youtube.com/watch?v=JG_AMY_gSDQ
And when you've done this, you'll have access of every object in that cell through that class:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProgramCell", forIndexPath: indexPath) as YourCustomTableViewCell
cell.yourCellImage = cellContent[indexPath.row].yourImage
return cell
}
Also dont forget to link the tableView outlet from the storyboard to this viewController and set the DataSource to self (inherits from UITableViewDataSource)