I have a tableView with two different custom cell. One cell have few textfields data and other also have different textfield data,here i need to collapse and hide the each cell. i don't know how to do this.Please let me know.
https://maniacdev.com/2013/03/an-open-source-ios-control-allowing-you-to-create-great-looking-collapsible-lists-without-uitableview
Please refer the above link, i want the same in swift.
Thanks in advance
So you want to hide one of your UITableViewCells?
Ill think the easiest way is to add some more information to your "Row Data Array".
For example:
You have 10 Columns, and you want to hide 4 Specific Column. You have a function called "getRowData" - this returns (when no filter is selected) all 10 Rows. When you tap on a button, you could change your "getRowData" function to return only the 4 Rows you want.
With the example you showed above - you want some collapsible Lists, based on an UITableViewController.
So you have (In this example) 3 Main Categories - and if the User click on one "Title" you want to expand the list for this Category.
So you could use Sections - create a Section for each Category. Then implement something like this:
// create a main variable for your "activated Category"
var enabledCategory:Int = 0 // you could add i base value
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
enabledCategory = indexPath.row;
....
Check here which section was clicked by the user. If (for example "0" - so the first one, expand this section. So do something like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == enabledCategory) {
return rows // return rows you want to show
}
You need also to change the count of rows in here:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
this should be (in your example) always 3.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
This depends on your section // Category
Related
After much searching and reading I unfortunately do not come from the following. I want to use static tables to display certain data. (Are there better options?)
In my view I first put an onion picture with a container view underneath. The container view again refers to a Table View Controller.
I made an outlet from the cells and then I thought I could easily adjust the text.
Now I want to change the text of the fields in the table, but unfortunately I do not succeed.
When I start the app then the table is completely empty as seen on the screenshot.
What am I doing wrong ?
class TableViewController: UITableViewController {
var data: [String] = ["Muis", "Aap", "Koe", "Vis"]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let iets = data[indexPath.row]
cell.textLabel?.text = iets
return cell
}
}
If you want to use static cells
Forget dequeueing UITableViewCell instances and all tableview data source and delegate methods.
In Interface Builder select the table view and select Static Cells from the Content popup
Drag the amount of static cells you need into the canvas
In the view controller declare IBOutlets and connect them directly to the UI elements in the cells
You need to change your way of thinking for this one. You do not own the cells, the UITableView does. It will provide cells as it seems fit by using your implementations of UITableViewDataSource:
func numberOfSections(in: UITableView) -> Int
func tableView(UITableView, numberOfRowsInSection: Int) -> Int
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell
Normally, the texts (your actual data) would be held in a list available to this data source.
Example:
var data: [String] = []
// Other functions
func numberOfSections(in: UITableView) -> Int {
return 1
}
func tableView(UITableView, numberOfRowsInSection: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_IDENTIFIER")
cell.text = data[indexPath.row]
return cell
}
Now, if you want to change this cell's text, all you have to do is update your data list and reload the data.
What I have done after a lot of testing and reading. I have create a segue to the statutable class.
if (segue.identifier == "myEmbeddedSegue") {
let childViewController = segue.destination as! hondDetialTableViewController
childViewController.hondId = hondData["hondId"]!
}
In this segue I send only the hondId, everything else i ask entities.
I'm sorry but this is not at all how UITableView works. The UITableViewCell that you define in the Xib/Storyboard within the tableview are just "models" or templates, they don't actually exists until you dequeue them.
You can read how UITableView works here: http://www.thomashanning.com/uitableview-tutorial-for-beginners/
You have to return numberOfSections > 0 if you want anything displayed in your tableview; similarly, that section has to also have numberOfRows > 0 otherwise again, nothing will be displayed (ok, maybe headers and footers if those are properly setup).
At any rate, cells are only accessible after you dequeue them. Creating an outlet in a XIB to a UITableViewCell is useless in most cases.
You can explore other options, such as UIStackView, or maybe what you need is just plain custom UIView with labels that you properly set and layout using NSLayoutConstraints. There are plenty of resources out there, this is just one I quickly Googled for you to get started: https://www.youtube.com/watch?v=de0sthle44I
Good Luck.
In my conception every row of tableview is kind of task which you can fill in.And every time you open the app one row should be a priori on a tableView. (As you see in picture )
Also I have button which allows you to add a row.
But I have no idea how can I know the number of rows in section.
Can you please help me with it ?(In my code I've commented the moments I can't understand)
class TaskSecondViewController: UIViewController,UITableViewDataSource{
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
self.tableView.separatorColor = UIColor.clear
#IBAction func insert_rows(_ sender: Any) {
let indexPath = IndexPath(row: 1, section: 1) // Don't know what to write in "row"
self.tableView.insertRows(at: [indexPath], with: .top)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // Here also
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TaskTableViewCell
return cell
}
}
have you tried this :
yourTableView.numberOfRows(inSection: 0)
this returns the no of rows in your table view's section
You can simply have
var numOfRow = 0
every time you hit the button
numberOfRow += 1
tableView.reloadData()
so you can return numberOfRow
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numOfRow
}
But I have no idea how can I know the number of rows in section.
The important thing to understand is that a table knows nothing about the items it displays, or how to display them, or what to do when people interact with them. A table is really just a list of sections, where each section is a list of cells. And it can scroll. And it can ask for help.
The items that are displayed are kept elsewhere. They don't even exist as rows in the table all at the same time. The table only keeps as many rows as it needs to display, and when the user scrolls, it asks some other object for more rows to display and throws out the rows that are no longer displayed. That other object is the data source, and it may be the 'elsewhere' where the items are kept, or it may only be an object that knows where to find (or generate) those items.
In this case, your TaskSecondViewController view controller is the table's data source. You need make sure that that controller somehow has access to the data it needs. Maybe it reads the list from a file. Maybe an array of items is passed in from some other object. There are a million variations on the theme, but it's up to you to know what you want to display in the table and to know where those things are kept. Once you know that, you should be able to figure out how many items are in a given section. You're also going to need to know how many sections there are. It could be that you just have one list of items, and you don't plan to break it up into sections; in that case, you'll just return 1 for the number of sections and the number of items in the whole list for the number of rows in that section.
If you just want to add a row, you don't have to insert it into the table view at an explicit spot. It would be easier to have an array (or dictionary or whatever) and add your items into that object, and then reload your table view when items are added.
var itemsToDisplay = [SomeObject]
override func viewDidLoad() {
// populate items if needed
}
#IBAction func insert_rows(_ sender: Any) {
// get your data to create your object
// add your object to itemsToDisplay
itemsToDisplay.append(myObject)
// for a TableViewController
self.tableView.reloadData()
// if you've included the tableView as an #IBOutlet
myTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsToDisplay.count // this might be zero, but it doesn't matter
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TaskTableViewCell
var item = itemsToDisplay[indexPath.row]
cell.titleLabel.text = item.whatever
return cell
}
I'm starting to work with UITableViews and can't seem to find out how to change the position of a cell with code. Changing the position in the storyboard is straightforward enough but I need to be able to do it in swift.
TLDR;
Update your data. i.e. swap(&arr[2], &arr[3]).
Call the tableView's reloadData() method to reflect the changes to your data.
Long answer
An instance of UITableView works by checking its data source (UITableViewDataSource) for the information it needs. This includes the number of sections and rows, as well as the instance of UITableViewCell that the table view is to use. These are defined by the following UITableViewDataSource delegate methods:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int;
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int;
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell;
Usually, you would base the former two on some data you have, likely an Array or similar container. For example, if your tableView displayed data from an Array named fruitArray (which contained names of different fruit - a list of strings), then you might have something like the following:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Our array is one dimensional, so only need one section.
// If you have an array of arrays for example, you could set this using the number of elements of your child arrays
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Number of fruits in our array
return fruitArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("yourCellId") // Set this in Interface Builder
cell.textLabel?.text = fruitArray[indexPath.row]
return cell
}
Then, you can see that the answer to your question becomes simple! Since the contents of a given cell are based upon fruitArray, all you need to do is update your array. But how do you get the tableView to "recheck" its dataSource? Well, you use the reloadData method, like so:
swap(&fruitArray[2], &fruitArray[3])
tableView.reloadData()
This then triggers the tableView to "recheck" its dataSource, hence causing your data swap to appear on the screen!
If you'd like the user to be able to swap the positions of the cells, you can use the following UITableViewDelegate (not UITableViewDataSource) delegate method:
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
Have a look at this article for more info. You can also view Apple's documentation on UITableView, UITableViewDataSource, and UITableViewDelegate for further detail.
Hope this helps!
I am building an app using swift and have created an array with over 700 rows displayed in the table view. The issue is that I need each row to occupy its own individual cell, but all 700 text strings are grouped to one table view cell. How do I go about separating each string to its own cell without manual creating and labeling 700+ new tableviewcells?
EDIT: Thanks for the help! It seems I really didn't understand how table views worked. I've now made custom tableviewcells and should get the result I'm looking for this way.
Assume you have your array let myData = ["item 1", "item 2", "item 3"]
Make sure your UIViewController inherits UITableViewDataSource
Override the following methods and modify accordingly
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Should ideally get UITableViewCell using dequeueReusableCellWithIdentifier
var cell = UITableViewCell()
cell.textLabel?.text = myData[indexPath.row]
return cell
}
I have an UITableView with custom cells. I would like that the first row is always visible. As I have only once section, I thought of making a header but in this case I don't really know how to do it?
Is it possible to make a header from the first row with the same gesture recognizers, same dataSource behind the rows, briefly, have the header exactly like th row, just as if the row was pined to the top of the tableView?
You should use a header, or a separate view outside the table view. You can use the same gestures (generally, though not the delete) and data source.
If you want it all, you could use 2 table views- the first with one section and one row, the second with all the other data. It would be easiest if your data source was broken down in a similar way in the view controller.
In either case you can achieve what you want, but not by flicking a switch, you will need to add some logic and different views to make it happen.
If you want to make one static cell that is pinned to the top but in all other ways the same to the others, you could simply add one to your numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count + 1
}
Then when you display the cells, check for the row number and always set the first row to contain your static header content.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
// Create or set static cell content.
}
}
The other way is to create a custom section header and set it using viewForHeaderInSection
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
var view = UIView()
view.backgroundColor = UIColor.blueColor()
return view
}
return nil
}