I build a social app, now I want to make some good appearance. I want to change tabs Contacts / Rooms from segmentedCntrol. How can I do that with correct form? I have two ways...Clear contacts table view and fill it with Rooms data. Or hide view with a table view and display another view with new tableView.
You should create 2 tableViewCells and switch between them in cellForRowAt according to selected segment
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if mySegment.selectedSegmentIndex == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier:CellIdentifier1) as! Cell1
// setup here
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier:CellIdentifier2) as! Cell2
// setup here
}
}
Related
I have a TableView that I want to reuse for different categories of data (essentially as plugins.. the tableView being a skeleton and being filled with whatever I want it to be). The TableView is filled with different categories of data (and related actions) depending on essentially what ViewController the user came from. I understand how to make it display the various data (just send it whatever array I want it to display), but I can't figure out how I could control the actions for the data at the specific index selected in didSelectRowAtIndexPath.
How could I do this? How could I create an array that has both Strings and executable actions associated with each indices? For example:
arrayOneNames = ["Tigris", "Leo", "Barko"]
arrayOneActions = [displayTheTigers, displayTheCats, displayTheDogs]
If "Leo" is selected in the tableView, then "displayTheCats" is executed. Again, I want each array to be a separate Class that I can use as a plugin, so that I can fill the tableView with whichever Class of data I want it to display and execute, depending on which ViewController the user came from previously. Please answer in Swift.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=UITableViewCell() // cell you've created
cell.data = arrayOneNames[indexPath.row] // passing relevant data
cell.tag = indexPath.row // the tag you want to pass for particular data
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)! as UITableViewCell // take selected cell
if(cell.tag == 0) { // call relevant function accordingly.
self.tigrisTouch()
} else if (cell.tag == 1) {
self.leoTouch()
} else {
self.barkoTouch()
}
}
private func tigrisTouch() {
}
private func leoTouch() {
}
private func barkoTouch() {
}
In this screen how to show (blue view) in between tableview rows
design image
code: in storyboard design i have given all static data in labels and images so with this below code i am getting all cells like above screen shot, but after three cells how to show blue box view, please suggest me
import UIKit
class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
There are two ways to do that:
Use a different UITableViewCell class (probably what you are looking for?)
Use sections
How?
You can either create a new UITableViewCell prototype cell in your storyboard or do it programmatically.
Create a custom UITableViewCell as such:
class OfferTableViewCell: UITableViewCell {
}
// If you are not using storyboards, add the following code
// in your viewDidLoad
tableView.register(OfferTableViewCell.self, forCellReuseIdentifier: "your_cell_id")
Then, you can dequeue the newly created cell at any index as such:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 10 // Index of the different cell
let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id", for: indexPath) as! OfferTableViewCell
// Do cell configuration here
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
}
Keep in mind that this cell is going to take the place of another cell if you are using an array as datasource, so using myArray.count for your numberOfRowsInSection would result in the last array element missing. You will have to take that into account.
Resources
Using multiple custom cells in a TableView
UITableView sections
Custom header for TableView sections
#objc func addRow(sender: UIButton!) {
print("add")
names.insert("New Name", at: 0)
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .right)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
names.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// if section == 1 {
// return carArray.count
// }
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: bondeCellId, for: indexPath) as! BondCell
//
// if let lbl = cell.contentView.viewWithTag(101) as? UILabel {
// lbl.text = names[indexPath.row]
// }
//
// if let btnDelete = cell.contentView.viewWithTag(102) as? UILabel {
// btnDelete.addTarget(self, action: #selector(deleteRow()), for: .touchUpInside)
// }
return cell
}
enter image description hereI am fairly knew to programing and don’t know a lot.
I have built table view that lets the user add more table cells if they press the add button.
So the user will add more table views on depending if more information is needed.
The problem is to understanding is how to pass all info from multiply table views cells with the info to a new view controller.
If the User has insert 5 or 10 table cells I want those table cells info to be passed into a one new view controller.
The table view cell contains two text fields and two labels and the text field contains UIpickers and depending what they chose it updates in the labels in cell tables.
But I also want sum of all the table views chosen by the user to be added to a chart that I am making to a new view controller.
Would the labels and the text fields need to get new names each time a new table cell is made?
I am stock with ideas
If the User has insert 5 or 10 table cells I want those table cells info to be passed into a one new view controller.
The code you posted is a mess, and not very helpful with your question. It's mostly boilerplate.
You are thinking about this wrong. Cells don't store data. More generally, view objects don't store data. They display data.
You need a model object that stores the data for your table view. It looks like your table view only has one section, so a simple array would work just fine. Create a struct to hold the info you collect from the user. Let's call that struct CellData. Your model would be an array of CellData structs.
As the user creates new cells, you'd collect the new info and add it to your array.
Then when the user selects cells, and invokes the new view controller, you'd pass the whole array, along with the indexes of the selected cells, to the other view controller. (or perhaps just the structs for each selected item.)
I have a collection view with 4 cells and a table view inside the same view controller. Each cell in the CV must make the table view display a different list; one table cell type per CV cell. When the user taps on one CV cell the table must reload its data and display the correct type of cell, (basically a different list with a custom cell). Each of these lists contains only 1 section.
I created and registered all my different cells with the table view and have all my data ready, with the views set and so on. How do I make the table view discard its current cells (list) and show a new list with different cells when the user has tapped on another CV cell?
I suppose that the solution is in the didSelectItem method from the CV delegate but I cannot find any information that shows how to make the table dequeue a different type of cell when the user has changed the cell selection inside the CV; or discard the previous one if needed.
At the moment I only register and dequeue one type of cell, and inside the delegate method for the CV I am calling empty functions that are supposed to put the new list inside the table.
The number of rows for each list is dynamic and this implies that I would have to call the delegate method on the table view again.
I have found an example of the MVVM pattern but I cannot apply it to my logic as that is more static.
Any help would be much appreciated. Thank you.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedMenuTab = indexPath.item
switch selectedMenuTab { // show different type of cell.
case 0: showAList()
case 1: showBList()
case 2: showCList()
case 3: showDList()
default:
print("no such main tab")
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableOfConversations.dequeueReusableCell(withIdentifier: rowID, for: indexPath) as! ConversationsListCell
let messageInChatList = listOfOneToOneChats[indexPath.item]
cell.messageInChatList = messageInChatList
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfOneToOneChats.count
}
I think this should be as simple as calling tableView.reloadData() at the end of the collection view delegate's didSelectItemAt method.
The tableView should have it's data source based on a common array, for example:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return displayingList.count
}
And then in the didSelect of the collection view set that array and then tell the tableView to reload:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedMenuTab = indexPath.item
switch selectedMenuTab { // show different type of cell.
case 0: displayingList = listA
case 1: displayingList = listB
case 2: displayingList = listC
case 3: displayingList = listD
default:
print("no such main tab")
}
tableView.reloadData()
}
And for dequeueing cells, check based on the type if they are different, or based on selectedMenuTab:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UICollectionViewCell
let selectedMenuTab = indexPath.item
switch selectedMenuTab { // show different type of cell.
case 0: cell = tableOfConversations.dequeueReusableCell(withIdentifier: rowID, for: indexPath) as! ConversationsListCell
//And so on
default:
fatalError("No cell for this tab")
}
let item = displayingList[indexPath.item]
// Cell setup
return cell
}
The type of the values will be something to consider in order to avoid an array of Any, which would not be super type-safe, but that will depend on the types of your objects.
I want to send data to a tableview in a another view controller to cells 1, 2, 3,etc everytime I hit the button to transfer the data. I can send the text from the label to transfer to the tableView only to the first cell. but when i go back I want that cell to keep the text and then save the new label text in the second cell and then go on and on.Thanks
This is the code from the second view controller where the tableView is. right now its only working for the first cell.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = data[indexPath.row]
return cell
}