I struck in tableview. I need tableview of collapse and Expand looks like Below,
]2
I need to add 2 buttons for last row of every section.
Could you guide me how to design the below design in ios swift?
Add your buttons in custom view and feed it in UITableViewDataSource method below:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
Best way is take Two Button in one cell, and return that cell when indexPath.row = lastRow
And add tap gesture on header view for collapse view
You can create a cell with two buttons. Next you should add it to your data source and configure it properly. Then add a delegate to your custom UITableViewCell and handle taps.
Related
I currently have a table view controller which consist of not only the cells but also a UIView. Now, within that UIView there's a label which might have more than 1 line of text with a See More button. When I pressed that button, the button itself will disappear and the text.numberOfLines is set to 0 so the View should expand to show all text. Which doesn't seems to work in my case, The button does disappear though and the text just continues to the edge of the screen, truncated instead of extending down.
But when this whole UIView is outside the table view controller the functions above work just as expected, but not after I've moved them to within the table View right above the prototype cells. Any Ideas?
When you are setting up the tableviewcell, you have to specify a fixed height for the row with the delegate function
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) {
if expandedArray[indexPath.row] {
return 60
} else {
return UITableViewAutomaticDimension
}
}
Then keep an array of bools, to specify if the cell is expanded or not. Primarily set the bools to false indicating "not expanded".
Then when the use presses the "see more" button, make the bool in the array with the right index true, indicating that the cell is expanded. and call the below code updating the cell.
tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
tableView.endUpdates()
That should do the trick.
P.S: Don't forget to properly add constraints inside tableview cell.
Use UITableViewAutomaticDimension and reload your cell on click of see more button.
I made sidemenu. but i have some question. when i clicked button on the sidemenu, appear an other side button.
look like this image
when i clicked sidemenu button, appear subSidemenu.
I don't know how to say this function.
Firstly if you want to show a list of values which are collapsable then you need a UITableView. Make your SideMenus as sections and rows as SubSideMenus.
And you would like to have that feature when user tap so implement tableview's numberOfRows method like this
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sideMenu = mySideMenus[section]
return sideMenu.isCollapsed ? 0 : sideMenu.subSideMenus.count
}
And when the header is tapped you need to call tableView.reloadSections(sections: [IndexSet], with: UITableViewRowAnimation) to reload that section.
I don't know if this is was your exact question but sidebar is easy to implement for your app. And you don't need buttons when working with tableView.
I have a tableView with hundreds of cells. I want to add a feature to pin to top favorite cells, for example by swipe. Is there a way to do that? Or is it easier to have a different tableView with favorite cells?
Section for this.
Add your favorites to view, you can create it using storyboard or programmatically using following code. If you use storyboard, create and make a reference to the tableViewController and just return in this code.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
// create and return yourView
}
I have a tableview with many sections. I am using an actual tableview cell which I dequeue to use as a custom section header. The issue I am having is when I scroll the section header "sticks" to the top of the table until the next section appears.
How can I prevent this from happening and actually have it scroll up like normal?
Here is my code
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell
return cell
}
return UIView()
}
Do following steps.
From #IB
Select your tableView
Go to Attribute Inspector
Find Style Attribute drop down which is Plain by default
Change it to Grouped
Or if you are creating TableView Programmatically use
let tableView = UITableView(frame: YourFrame, style: .Grouped)
Change UITableViewStyle to UITableViewStyleGrouped.
Set UITableViewStyle to UITableViewStyleGrouped, the headers will scroll up with the cells.
and
func tableView(_ tableView: UITableView,
heightForFooterInSection section: Int) -> CGFloat
{
return 0.000001;
}
Step1: Go to Mainstoryboard
Step2: Click on Your Tableview
Step3: Go to Attribute Inspector
Step4: Find Style Attribute drop down which is Plain by default
Step5: Change it to Grouped
Some possible solutions :
If you have just one section then set the Tableview Header property to the UIView you want to set as the Section Header.
If you have multiple sections, change the row count for each section to (number of rows for that section + 1) and make the row at 0th index to be a header. Some hand coding will be required.
Change your tableview to grouped, will require UI redesign so that the design looks similar to a plain tableview cell.
Instead of UIHeaderViewCell, You should use a UITableViewCell. Make section header height as 0 and for every 0th item in that section , display a UITableViewCell with heading on it
I am trying to change the value of an element of an array depending which section a button is clicked in. For example say I have this array numbers = [0,0,0,0,0] and I want to change the first element to 5. I insert 5 into the cell of the first section and click done in that same section and the array will now read [5,0,0,0,0]. Is there a way to know which section the button belongs to?
Right now I have two separate classes. One for the custom cell and one for the tableview. Both of the have an outlet to the button. When the button is clicked the custom cell class changes a temporary global number to the inserted number. And inside the table class I want the button action to take that global number and insert in into the element that is the same number as the section the button belongs to. Except I don't know how to find out which section it belongs to.
Can anyone help me out with this? I'm writing in Swift btw.
If you only need the section (not the section and row) of the index path of the cell containing the tapped button, you could tag the button with the section number (Int) when you configure the table view cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath:indexPath) as! MyCustomTableViewCell
cell.myCustomButton.tag = indexPath.section
// Remove all existing targets (in case cell is being recycled)
cell.myCustomButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
// Add target
cell.myCustomButton.addTarget(self, action:"buttonAction:", forControlEvents:.TouchUpInside)
return cell
}
func buttonAction(sender:AnyObject)
{
if let button = sender as UIButton{
println("Tapped Button in section: \(button.tag)")
}
}
If you need both the section AND the row, you're better off storing the whole index path. For that, you can either use a custom UIButton subclass with a property of type NSIndexPath, or store it in the table view cell.
But the second solution is a bit messy since you have to access the table cell (and then, the index path) from the button by using superview, etc., and this relies on the table cell's subview structure. I'm not sure if the parent view of your button is the table cell itself, or its "content view" (I'm a bit rusty right now...)
If you want to find which cell has been 'clicked' you could use the UITableViewDelegate method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}