create drop down in UITableView swift - swift

I have gotten to the point where I have a selectable list of options, which are all loaded into the same .xib files. I am trying to make it so upon the click on a cell a different .xib file is populated below, and all of the other cells shift down. However, I am unsure of how to have 2 different xib's as TableViewCells in swift.

There's two ways I can think of to accomplish what you are trying to do:
Single xib file: You should use a single xib file that hides the lower section that contains the options until the cell is tapped. When tapped, a property on the UITableCell class could be set, e.g. isExpanded. When isExpanded is set, your cell could unhide the options (by changing constraints).
Multiple xibs: When tapped, you could insert a new item into your datasource. In your cellForRow, you could check for some identifier and load either the regular cell or the options cell using something like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == optionsRow {
let cell = tableView.dequeueReusableCell(withIdentifier: OptionsCellIdentifier, for: indexPath) as! OptionsCell
//Configure cell here
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: NormalCellIdentifier, for: indexPath) as! NormalCell
//Configure cell here
return cell
}
}

Related

UITableViewCell title positioning

I am creating an app with UITableViewCells that expand when selected. However, when the cell expands, the title (and subtitle) are moving downwards to stay centered vertically in the cell. I'd like them to stay where they were originally but can't figure out how to do it.
I have tried setting the frame manually with cell.textLabel?.frame = myRect, but this does nothing. I am unable to find any constraints, as print(cell.textLabel?.constraints) returns optional([]).
Here are my normal cells and my expanded cells.
My code for creating the cells:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
if cell == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: "reuseIdentifier")
}
cell!.textLabel?.text = content[indexPath.row]
cell!.clipsToBounds = true
cell!.selectionStyle = .none
cell!.detailTextLabel?.text = detailContent[indexPath.row]
return cell!
}
Any help would be greatly appreciated.
You are using a standardized cell, the title and detail are fixed, i think you need make a custom cell for your project
Look this project I make a simple test with customized cell
https://github.com/TexugoProgramador/Teste-Celula-Customizada

I have a UItableView cell with a Image but can't get a image click to allow parameters

I have a EDIT2 UItableView, in Swift, with the normal labels and relevant Information. I have added a Image to the cell and all is working except when I click on the image I would like to open another ViewController with that particular cells relevant information. I have used:
let popup: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt))
cell.alrmwarning.addGestureRecognizer(popup);
as the means for calling the pushViewController function, But the Viewcontoller requires a parameter AlarmID to be set Beforehand. Said Viewcontroller needs AlarmID to fetch its relevant information.
Edit1 The parameter is dependant on the selected cell.
Create a closure variable in cell:
class SomeCell: UITableViewCell {
var imageClicked: (() -> Void)?
}
then in action of your tap gesture self.popupalrmcontrnt
add this line
func popupalrmcontrnt() {
self.imageClicked?()
}
then in your cellForRow, you can access this as:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
(cell as? SomeCell)?.imageClicked = {
//Navigate to your view controller here
}
return cell
}
Hope it helps!!
Better use UIButton. you can set image to the button as background image. Also you can pass relevant info as an object to the new ViewController.

How to save the state of UITableViewCell accessory checkmark in Swift 3

I have a table view and I would like the user to select only one item from the table view, then I want to add a checkmark to the cell. At the same time, I also want the checkmark on other cells to disappear so the. user can only select one at a time. Then I want to save the data (preferably using UserDefaults). How do I do that?
Create a class variable of type IndexPath. In your cellForRowAt method set .checkmark accessory type only if index path matches the class variable and .none otherwise. In didSelectRowAt method update the indexPath of selected variable and just reload the tableview
you should Use in this way
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let sgp : UITableViewCell = tableView.cellForRow(at: indexPath)!
sgp.accessoryType = .checkmark
}

Putting multiple type TableViewCells in the same Table View

I have a table view that is currently displaying a type of tableViewCell creating in a XIB File and I want to display a different type of XIB file in the table View at random Places. e.g. Like having Ad display Cells in a tableview at Random Post positions.
How can I do this. I currently have the following code for displaying one Cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cardioCell:CardioItemsTableViewCell! = tableView.dequeueReusableCellWithIdentifier("CardioItemsTableViewCell") as? CardioItemsTableViewCell
cardioCell.cellNameLabel.text = "Test String"
return cardioCell!
}
I have this image of the apple news App with one cell without an image. In my instance though, the cell is completely different and has a different layout to the other cells.
Any ideas would be much appreciated.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row % 2 == 0
let cardioCell:CardioItemsTableViewCell! = tableView.dequeueReusableCellWithIdentifier("CardioItemsTableViewCell") as? CardioItemsTableViewCell
cardioCell.cellNameLabel.text = "Cells with even number of row"
return cardioCell!
else if indexPath.row % 2 != 0
let otherTypeCell:OtherTypeTableViewCell! = tableView.dequeueReusableCellWithIdentifier("OtherTypeItemsTableViewCell") as? OtherTypeTableViewCell
otherTypeCell.cellNameLabel.text = "Cells with odd number of row"
return otherTypeCell!
}
You can not load cells randomly. You need to have a certain logic based on which you can load different types of tableViewCell.

Retrieving Information from Specific UICell Using Swift

In my program, I have an UITableView and I need to access a label in one of the UITableViewCells. I have the indexPath for the UITableViewCell, but I was wondering how I should proceed in grabbing the cell's label using Swift. Any ideas on how you access a cell without being in the table view swift file?
You shouldn't go through the cell to grab the content of the label. Instead, you should go through whatever data structures that you used to set that label in the first place.
Go to your tableView:cellForRowAtIndexPath: code, and find the part where you set up the label in the cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row] // <<== HERE
return cell
}
You have code similar to the above, where you use the source of data to set up your label from an indexPath. Using the same code to obtain the text that went into your label provides the most direct way of getting the data.