Swift - How to get Segmented control to lock at top of screen - ios

I have a UI ImageView and a few labels on the top of the screen, followed by a segmented control, with a tableView underneath.
How would you go about making the segmented control lock at the top of the screen, if the user scrolls down in the tableView?
Should everything be embedded in a scroll view, even though tableViews already have scrolling?

Set number of section in :
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
Then in :
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0:
return 0
default:
return "According to segment conrtrol height"
}
}
for first section header height will be 0 and for second section header height according to the segment control height.
Then design the UI for segment control in :
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0:
return nil
default:
"Create your UI"
}

Related

How to display title for header text for header in first section iOS

Using method below of viewForHeaderInSection i try and display an image in table header section one , but i also want a heading text, using the method of heading title text i am able to display the heading for all sections but not section 1 , do i need to make some changes to heading view for section one to be able to display the heading , if i were to remove the viewForHeaderInSection then the title for first header section also shows up
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerTop.headerLogo.image = UIImage(named: "foodpin-logo")
return headerTop.topView
}
return nil
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section]
}
You need to create a text label in your headerTop view and remove this function override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int). So your code will be like this:
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerTop.headerLogo.image = (section == 0) ? UIImage(named: "foodpin-logo") : nil
headerTop.yourHeaderTextLabel.text = sectionTitle[section]
return headerTop.topView
}
It seems like you are looking for tableHeaderView! The issue is you are overriding the first section's header.
You may consider setting the tableView's tableHeaderView for the headerTop instead. So all sections will have their titles as you expected.
tableView. tableHeaderView = headerTop
Also, if you want yout table's header to be sticky, there are bunch of tutorials for that.

Table View Height Dynamically

I implemented expandable sections on my UITableView and now my problem is that when the UIViewController loads the table there are rows below the section.
Not expanded:
Expanded:
I want the UITableView to have no rows if the section is not expanded so I can set another UIView below the section. When the section is expanded the UIView should disappear and the table view to be presented on the whole screen.
Simply implement the viewForFooterInSection and return an empty view.
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: .zero)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
just use this code it will remove your unwanted rows.

ios tableview section header is not sticky

I have a tableview and tabview (3 different tab).I want to show 4 section for first tab , 3 section for second tab and . 2 section for third tab.
Just first section's header must be sticky top of the view.Because of this I have implemented headerview just first section but header scrolls and be hidden like a tableview cell .it is not stick on top of the screen.What is the problem here?.Must I implement or override a specific function of tableview?
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0{
return UITableView.automaticDimension
}else {
return 0 //sadece 1. sectionda tablar header olarak olacak diğerlerinde header olmayacak
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func numberOfSections(in tableView: UITableView) -> Int {
if dataReady {
totalSectionCount = getSectionCount()
return totalSectionCount
}else {
return 1 //permanent because of tabview
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if !dataReady || ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure{
return 1//for shimmer cell and semisecure view
}else {
return getNumberOfRows(sectionNumber: section)
}
}
There is not any function which restricts Specific Header To Stick and Specific To Scroll. If you defined a header for a section, it will scroll up and will be hidden when next section header comes up.
In your case, you must define first header view/cell as Section Header and manage other headers in cellForRowAt() method. Because you want them to scroll up and not stick at top.

swift - create a page for both repeated data and for no data

I need an advice with ViewContoller's scheme. I should create a view with Billing addresses. There can be no address at all, or some addresses. If there no one, there should be only a button "Add New". And if there are addresses, each should have buttons Edit, Remove, and "Add New" too.
I have data for this VC as JSON, parsed and saved to plist.
So what is logic to make this View looks different depends on 1) if there are addresses or not? and 2) if there 1, or 2, or maybe 20 billing addresses?
Thanks a lot!
I solved issues like this with UITableVIew and the UITableViewDataSource and UITableViewDelegate:
setup the table view for one section (adresses)
func numberOfSections(in tableView: UITableView) -> Int {return 1;}
return the address arrays length in the delegate method
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return adresses.count
}
set the view for footer if the arrays length is 0
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if adresses.count == 0 {
let vw = YourViewClass()
//I use blockskit library here (vw.bk_) to recognize a tap, but you can add a button by yourself
vw.bk_(whenTapped: {
//Create and present your next viewcontroller to
})
return vw
}
return nil
}
set the footers height to 0 if there are addresses
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if addresses.count > 0 {
return YOUR_DESIRED_FOOTER_HEIGHT_FOR_INPUT
}
return 0
}
create row for each address
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let address = addresses[indexPath.row]
let tableViewCell = UITableViewCell() //maybe you have to create your own if the layout does not fit
//set tableViewCell's title / description to show address values
return tableViewCell
}
In this case, the footer view (you can do the same in the header if you want) with the add button is shown when no addresses are available, and it is hidden, when addresses are available.

UITableView headerView different sizes

I'm trying to apply a size on my UITableView headerView but it seems like the first section header always is a bit smaller than the rest section headerViews. I've said the constraints for the UITableView so it should be fine. How come the first section is smaller?
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return ""
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
This behavior occurs when using a Grouped UITableView. The section header height appears shorter for the first section header, because it doesn't have a footer above of it. However, every section header after the first one, does have a footer directly above it. The footers have a default height, which is contributing to the overall height appearance of the section header. Therefore, set the section footer heights so the space between the group sections will have the same visual height.
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.min
}
We use CGFloat.min instead of 0, because returning 0 will cause the footer to return a default height.

Resources