I have two table views containing data, the first one is completely okay. I want to push from this table view to another view to another table view, but I cannot set the rows for it. How can I navigate through a multidimensional array in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Basically, my problem is about setting the right text for a row and getting from a multidimensional array :) (P.S. I have a different number of Strings in each subarray)
You just need to check on which tableView you are setting the values.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
if tableView == myFirstTableView {
// put the data for the first tableView
} else {
// put the data for the second tableview
}
}
Related
I want to implement cell value check on tableview cell for specific rows ( only to those fields which are mandatory) so alert generate or print command, cell row background color will be set to red and user must have to fill that values before posting values to Server. Which function I have to use for it to check cell value status? Also I am using sections and rows structure in tableView.
For posting changes to server I am using button. Guidance suggestions required.
1.Save temporary data about cells which have mandatory fields to be filled.
2.Using this data, check whether user filled or not in below method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
3.Keep updating temporary data whenever user interacts with cells.
Hope this helps.
write your validation check logic code in:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { }
and when you want to check just reload tableview
I have a list of custom objects [Species] displayed in the table view, which is sorted alphabetically. Table has one section with no headers, it's one continuous list.
What I would like to achieve is, when a user selects the option to sort the data "by country", to do the following:
to sort array to find out how many sections I will need by - "Species.country"
to create sections with header titles of the country
to sort countries (Sections) alphabetically
reload table view to display sections
remove sections on the reversed action (sort entire list A-Z)
Is it possible to create dynamically sections when filtering/sorting? Can you please point me the right direction? Many thanks
A.
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.genusArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomMenuCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomMenuCell
//genusArr is type of [Species]
let genus = self.genusArr[indexPath.row]
cell.populate(with: genus)
return cell
}
It's all in your data model. You always have sections with rows. But you only have one section. Modify your data model and your data source methods to always support multiple sections. Just sometimes this data model will only have one section.
Update your data model to be an array of dictionary where the dictionary contains a title and an array. The top level array is your sections (may just be one at times). The inner arrays (in each dictionary) are the rows of each section. Or define a struct with a title and array instead of using a dictionary.
With this in place, and your table view data source and delegate methods written to always work with multiple sections, your table will also work just fine when you happen to have just one section worth of data.
Now it's just a matter of populating your array of dictionary as needed depending on how you wish to organize the data for display.
To answer the question of section headers. You could try using:
tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
or
tableView(tableView: UITableView, viewForHeaderInSection section: Int)
How can I ignore the first item in an array when display in a TableView?
What I want is to ignore the first item when presented in a UITableView, I DON'T want remove it just not show it in the TableView.
The following code show all items from the array in the TableView.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
let data = lists[indexPath.row]
cell.textLabel!.text = data.listName
return cell
}
Your data source methods are using lists as their basis, and you don't want to do anything that mess that up. The numberOfRowsInSection and the cellForRowAt need to stay in sync.
I can think of two possibilities:
Keep the real model elsewhere, and keep in lists only the part of the model that you want to include in the table.
Or (a whole different approach) implement heightForRowAt to give the undesired row a zero height.
I have a ViewController that contains a collectionview and a tableview. I would like to be able to press a row in my tableView and update the collectionView with new images. I'm not sure how I can reference the collectionView in this scenario.
I'm assuming I would have to put some code in my "DidSelectRowAt" method in my tableView, and pass data using dictionaries. Anyone have any idea? thanks!
Try this method and pass your latest dictionary to collectionView Dictionary
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
yourCollectionViewDict = arrDict[indexPath.row]
collectionView.reloadData()
}
Have you tried triggering collectionView.reloadData() from your tableView's didSelectRowAtIndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
collectionView.reloadData()
}
I have implemented two custom cells in the tableview.Now I want to replace these two custom cells with one another. How to achieve this?
Implement following tableview methods and write your code in it
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// return boolean value for a specific or all cells, you want to enable movement
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Handle array element movement along with your row/cell
}
Share your full source code better help
If you would like to reorder rows in UITableView. Take a look at two delegates that are implemented in the UITableView.
They are: tableView:canMoveRowAtIndexPath: and moveRowAtIndexPath:toIndexPath:.
Also take a look at tutorial that show how it is possible to implement.
Along with tableview delegates, use moveRow(at: <IndexPath>, to: <IndexPath>), to move your row programatically (automatically), without user interaction.
tblTabel.moveRow(at: <IndexPath>, to: <IndexPath>)
// Tableview Delegates
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// return boolean value for a specific or all cells, you want to enable movement
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Handle array element movement along with your row/cell
}