How to disable all cells except the selected one - ios

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 .

Related

how to avoid reusable custom cell from overwriting data when scrolling?

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
}

Expanded Cell not being clicked

In my app i have a tableview with a custom cell which contains a collectionview and a button in it.I want to implement expanded functionality in my app.The issue is that when the expand a paticular cell, the collectionview data and button are not being clicked.I found out that when i fixed the table height, my collectionview and buttons are getting clicked but when i calculate the expanded height and assign it to tableview at that time its not being clicked.
Code
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YourBudgetTableViewCell
cell.budgetIcon.image = UIImage(named: "ic_your_budget_close");
// cell.collectionView.tag = indexPath.row;
// cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
cell.selected=true;
isCellSelected = indexPath.row;
self.tableView.beginUpdates();
self.tableView.endUpdates();
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YourBudgetTableViewCell
cell.budgetIcon.image = UIImage(named: "ic_your_budget_expand");
isCellSelected = -1;
self.tableView.beginUpdates();
self.tableView.endUpdates();
}
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
if selectedCell.selected {
self.tableView.deselectRowAtIndexPath(indexPath, animated: false);
self.tableView(self.tableView, didDeselectRowAtIndexPath: indexPath)
return nil;
}
return indexPath
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if isCellSelected == indexPath.row
{
return 200;
}
else
{
return 44;
}
// return 200;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listBudget.count
}
So if i give fixed height say 200 in heightForRowAtIndexPath the click works fine, but if not given fixed click is not working
Check for the height for the expended cell or set alternate background color to cell. You will see that cell does not have that height which is required to display CollectionView. Because of that you are not able to click on the buttons at bottom of cell.

Hide element on select within table cell - Swift

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

UI table view cell check marks repeating swift

My when a table cell is checked and you scroll down a check mark is repeated.
I know this is due to cell reuse, but don't know how to fix it.
function to populate table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let item = self.myEvents[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = self.myEvents[indexPath.row]
return cell
}
//function to make the table checkable
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("indexpath: \(indexPath)")
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let text = cell.textLabel!.text
if cell.accessoryType == UITableViewCellAccessoryType.None {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
//let text = cell.textLabel!.text
if(checkedEvents[0] == ""){
checkedEvents[0] = text!
}else{
checkedEvents.append(text!)
}
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
var index = 0
for event in checkedEvents{
if event == text{
self.checkedEvents.removeAtIndex(index)
index++
}
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
First, you need to store the number of the selected row somewhere. How about self.selectedRowNumber?
var selectedRowNumber: Int? = nil
Set this when the user selects a row (short version):
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = .Checkmark
self.selectedRowNumber = indexPath.row
// You'll also need some code here to loop through all the other visible cells and remove the checkmark from any cells that aren't this one.
}
Now modify your -tableView:cellForRowAtIndexPath: method to clear the accessory if it's not the selected row, or add it if it is:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = self.myEvents[indexPath.row]
cell.accessoryType = .None
if let selectedRowNumber = self.selectedRowNumber {
if indexPath.row == selectedRowNumber {
cell.accessoryType = .Checkmark
}
}
return cell
}
This code was written here in the browser, and may need some fixes to compile.
If you want only one selection, put tableView.reloadData() in your didSelectRowAtIndexPath function

How do I show a hidden subtitle in a UITableView when selected?

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
}

Resources