Is there a way to hide the subtitles of all the cells until you select a cell - then it only shows you that cell's subtitle? I tried the following code - which successfully hides all the subtitles but fails to show one when I select a cell:
if cell.selected {
cell.detailTextLabel?.hidden = false
} else {
cell.detailTextLabel?.hidden = true
}
Thanks for any help.
Edit 2 - I ended up doing this in my didSelectRowAtIndexPath:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
for cell in tableView.visibleCells() {
cell.detailTextLabel??.hidden = true
}
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.detailTextLabel?.hidden = false
}
Thanks very much, Christian!
Just use the didSelectRowAtIndexPath method and access the touched cell. Then you can show the detailTextLabel.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
cell.detailTextLabel?.hidden = false
}
Related
How can I disable interaction with all cells except the selected one, and then enable the interaction after cell is selected again?
I tried like this inside didSelectRowAtIndexPath method:
if cell.isExpanded
{
UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
self.tableView.userInteractionEnabled = false
cell.userInteractionEnabled = true
}
}
But this will disable the entire tableView including the currently selected cell.
As of first your tableView need to select cell, after that one of the cell is selected you want other cell not to select except that selected cell, for that you can create one instance property of type NSIndexPath and use this to store inside didSelectRowAtIndexPath, and compare its value inside cellForRowAtIndexPath like this way.
var selectedIndexPath: NSIndexPath?
cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
if (self.selectedIndexpath != nil) {
if self.selectedIndexpath == indexPath {
cell.userInteractionEnabled = true
}
else {
cell.userInteractionEnabled = false
}
}
else {
cell.userInteractionEnabled = true
}
return cell
}
didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if self.selectedIndexpath != indexPath {
self.selectedIndexpath = indexPath
}
else {
self.selectedIndexpath = nil
}
tableView.reloadData()
}
Note: I have set the cell userInteractionEnabled to true again if you select your expanded cell.
Use
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
to findout the selected cell and store the indexPath selected in a variable and reload the tableview.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
disable all the cells except the one selected by checking the stored indexPath.
Hope this helps .
This is the code I am using and it works fine until I scroll in the table view, the cells overwrite each others and all the repostedFromLabel are hidden.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableViewCell", forIndexPath: indexPath) as? FeedsTableViewCell
if (arrayofPostsFeed[indexPath.row].postedByUser!.nickName!) != arrayofPostsFeed[indexPath.row].ownedByUser!.nickName! {
cell!.repostedFromLabel.text! = "Reposted From \(self.arrayofPostsFeed[indexPath.row].postedByUser!.nickName!)"
} else {
cell!.repostedFromLabel.hidden = true
}
}
how should I prevent reusable cells to override each others?
You can implement the prepareForReuse function in your FeedsTableViewCell to reset the hidden property of the repostedFromLabel. Something like this:
override func prepareForReuse() {
self.repostedFromLabel.hidden = false
}
At the moment you are not resetting that value, so it will get mixed up when reusing the cells.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableViewCell", forIndexPath: indexPath) as? FeedsTableViewCell
if (arrayofPostsFeed[indexPath.row].postedByUser!.nickName!) != arrayofPostsFeed[indexPath.row].ownedByUser!.nickName! {
cell!.repostedFromLabel.text! = "Reposted From \(self.arrayofPostsFeed[indexPath.row].postedByUser!.nickName!)"
cell!.repostedFromLabel.hidden = false
}else {
cell!.repostedFromLabel.hidden = true
}
I followed this app to build a table with custom cells: https://github.com/awseeley/Custom-Table-Cell
I am trying to expand a cell and show different content when the cell is clicked.
Here is what I have in my view controller:
var cellTapped:Bool = true
var currentRow = 0;
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
let selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TblCell
cell.image.hidden = true
tableView.beginUpdates()
tableView.endUpdates()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == currentRow {
if cellTapped == false {
cellTapped = true
return 141
} else {
cellTapped = false
return 70
}
}
return 70
}
The cell expands when it is clicked and shrinks when it is clicked again. But the image does not hide. How do I get the image to hide? Is there a better way to show another custom .xib file when the cell is selected and have an expand/collapse effect?
You should add this implement below to your cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == currentRow {
if cellTapped == false {
cell.image.hidden = false
} else {
cell.image.hidden = true
}
}
return cell
}
And didSelectRowAtIndexPath you should remove wrong code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
let selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
tableView.reloadData()
}
If you want use want use tableView.beginUpdate() for better animation, you can reference to my demo:
Demo hide image on cell
Hope this help!
I'm not familiarized with it, but you can look at Stack View. I think it's can help you doing what you want:
http://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views
Instead of the default selectionStyle where the background view changes color I'm trying to just change the color of a UIView which I've added to the guideViewCell. However, when deselecting the cell or pressing another cell it doesn't seem to apply the clearColor on the previous indicatorImage. I've tried to set the selectedBackgroundView to clearColor however this will hide my custom border. What can I do in order to solve this issue?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("GuideCell", forIndexPath: indexPath) as! GuideViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! GuideViewCell
cell.indicatorImage.backgroundColor = UIColor.blackColor()
}
func tableView(tableView: UITableView, didDeSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! GuideViewCell
cell.indicatorImage.backgroundColor = UIColor.clearColor()
}
My solution was just to create new variable and save the indexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! GuideViewCell
if (lastSelectedCell != nil) {
var oldCell = tableView.cellForRowAtIndexPath(lastSelectedCell!) as! GuideViewCell
oldCell.indicatorImage.backgroundColor = UIColor.clearColor()
}
lastSelectedCell = indexPath
cell.indicatorImage.backgroundColor = UIColor.blackColor()
}
You need to be sure the tableView delegate is set and the tableView allowsSelection or allowsMultipleSelection is set true.
I have a UITableView inside of another view. Whenever I click on a cell in the table, I would like that cell to be highlighted until it is clicked again to deselect it. Using the didSelectRowAtIndexPath method, I have accomplished this. However, the cell selection takes a long time. I have to hold down on a cell for 3 seconds before it highlights the cell, rather than it being instantaneous. How do I get it to select the cell the instant it is touched?
Here is my relevant code.
class AddDataViewController : UIViewController {
#IBOutlet weak var locationTableView: UITableView!
var fstViewController : FirstViewController?
let locationTableViewController = LocationTableViewController()
override func viewWillAppear(animated: Bool) {
// Set the data source and delgate for the tables
self.locationTableView.delegate = self.locationTableViewController
self.locationTableView.dataSource = self.locationTableViewController
// Set the cell separator style of the tables to none
self.locationTableView.separatorStyle = UITableViewCellSeparatorStyle.None
// Refresh the table
self.locationTableView.reloadData()
}
override func viewDidLoad(){
super.viewDidLoad()
// Create a tap gesture recognizer for dismissing the keyboard
let tapRecognizer = UITapGestureRecognizer()
// Set the action of the tap gesture recognizer
tapRecognizer.addTarget(self, action: "dismissKeyboard")
// Add the tap gesture recognizer to the view
//self.view.addGestureRecognizer(tapRecognizer)
}
}
class LocationTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.frame.height / 5
}
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
}
override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.blueColor()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel!.text = "Test"
return cell
}
}
The problem was the UITapGestureRecognizer was interfering with the tap of the cell. I apologize that the tap gesture code was not in my initial post as I did not realize that could be the culprit. I have added it into the code snippet in the original post.
This should work but it would be much cleaner to make a custom cell subclass.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")
cell.textLabel!.text = "Test"
let backgroundView = UIView()
backgroundView.backgroundColor = YOUR_COLOR
cell.selectedBackgroundView = backgroundView
return cell
}
Remove because we want cell selection
cell.selectionStyle = UITableViewCellSelectionStyle.None
Remove since this is already taken care of in the cell subclass
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
}
override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
}
I don't have enough reputation points to reply to #Jason247, so hence typing it as an answer. He's right. I had the same issue with a delay of the cells registering a click. Commented out the UITapGestureRecognizer and the delay went away.
In my context, I was using a UISearchBar. I replaced my UITapGestureRecognizer with the search bar's optional delegate method:
class ViewController: UIViewController, UISearchBarDelegate{
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
self.mySearchBar.endEditing(true)
}
}
You can find more solutions for dismissing the keyboard when using a UISearchBar here