after adding header cells don't appear - ios

I have big image and after this I have table view. This two elements are on scroll view, and table view :
scrollEnabled = false
I had two cells in my tableView, and their height was counting by function in cell file(cause text in cells is dynamic)
func configure(#comment: Comment) {
userCommentTextView.text = comment.comment
userCommentTextView.accessibilityLabel = "Quote Content"
userCommentTextView.accessibilityValue = comment.comment
}
all was ok, I was counting scrollview size like
scrollView.contentSize.height = tableView.frame.origin.y + tableView.frame.height
in `func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} delegate
BUT after I added custom header, all cells are not visible, there is only my header! Why? what is the problem? I checked, there is info for cells. And
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}
is NOT nil. But cellforrowindexpath isn't even executing. Can you help me?

Try adding your headerview by
func tableView(viewForHeaderInSection section: Int) -> UIView {}
and call
func tableView(heightForHeaderInSection section: Int) -> CGFloat {}
also
func numberOfSectionsInTableView() -> Int{}

Related

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.

How to show and hide Custom UITableView Header Cell of Table View in Swift3 iOS

I am developing an application in Swift3 where I have to show and hide UITableView Header for different users. For displaying UITableView Header View, I have created a custom class CustomHeaderCell of UITableViewCell.
Here is my code:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 235.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! CustomHeaderCell
return headerCell
}
Now Can anyone please help me to hide this Header of my UITableView?
Note: I tried using this tableView.tableHeaderView?.isHidden = true, but not working. Should I need to do the validation in heightForHeaderInSection?
Reference Link to Add HeaderViewCell: http://www.accella.net/knowledgebase/custom-header-and-footer-views-for-uitableviews/
If you have a way to differentiate users then you can just change the header height like this
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if userA {
return 235.0
} else {
return 0
}
}
That should help in hiding the header
You are mixing tableHeaderView and section header views, which are differents:
tableHeaderView is a view showed as a header for the whole UITableView
section header views are reusable views used for displaying header above each section
In your case, you want to use section header views, so you should return empty ones for non concerned users (I assume here sectionNeedHeader will be replaced by your condition).
Also, you better use UITableViewHeaderFooterView instead of UITableViewCell. The behavior is globally the same but it's made for this usage:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if sectionNeedHeader {
return 235.0
}
return 0.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if sectionNeedHeader {
let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! CustomHeaderView
return headerView
}
return nil
}

Swift - Static UITableView growing with UITextView

I have a static UITableView and one of my rows have a UITextView inside of it with autolayout pinned top to bottom left to right as 0.
I want the UITableView's contentView to resize depending on the content of the UITextView however I'm having difficulties working around this. I've scrapped the code I originally used as it wasn't working at all. Any solutions?
If the content of the textview is static.
Set SrollingEnabled to false in UITextView.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
If the content of the textview is dynamic.
Do the same thing as static and set the delegate for UITextView to self. Then in UITextview delegate.
func textViewDidChange(_ textView: UITextView) {
tableView.beginUpdates()
tableView.endUpdates()
}
This will definitely work.

heightForHeaderInSection only called once

Coding in Swift 3. Have a tableView with custom cells and header.
I have a tableView with custom cells and headers. The headers have two (2) labels in them and have dynamic cell heights since the labels may be long. My problem is the first time the tableView and sections are configured the label appears as it should, HOWEVER, after scrolling down and then back up the headers' layout somehow breaks.
As you can see below, after I scroll down then back up to the cells, the label is getting cutoff.
After printing out what methods are being called I found that the first time scrolling down the tableView the following two (2) override functions are called.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
print("\(section) heightForHeaderInSection")
print("\(section) returning AUTO for header")
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print("\(section) viewForHeaderInSection")
let header = tableView.dequeueReusableCell(withIdentifier: "QuestionHeader") as! QuestionHeader
header.delegate = self
header.contentView.backgroundColor = UIColor.groupTableViewBackground
header.questionTextLabel.text = String(questionStringArray[section])
header.questionNumberLabel.text = (String(section + 1) + ")")
return header.contentView
}
But when i scroll back up ONLY the viewForHeader function is called and I think because the height is no longer being set to UITableViewAutomaticDimension the labels get cutoff?
Any ideas?
You should return header instead of header.contentView from tableView: viewForHeaderInSection: method:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(...
...
return header
}

Let UIImageView in header section of UITableView overlap the content cells

I would like to have a header section in a table view with a logo and name. Now, I want the logo to overlap the image in the content cell below. I have attached an example of this below :-
Heres what I've tried so far, I've tried to make the header section half the size of (logo + upper and lower padding) -- this just cuts the logo in half
I also tried clipToBounds :-
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 33.0
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! FeedTableViewHeaderCell
cell.designerNameLabel.text = "Vitamin A"
cell.designerLogoImageView.image = UIImage(named: "zeko_small")
cell.designerLogoImageView.clipsToBounds = false
cell.contentView.clipsToBounds = false
return cell
}
You may set ImageViewFrameHeight greater than TableViewHeaderHeight and In your TableHeaderViewCell, set
TableHeaderViewCell.contentView.clipsToSubview = NO.
This'll definitely help you in getting your result.
Visual guide from XCode 7:
Please put this method and set your section's header height as you want.
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100// return height which is greater than your image's height.
}

Resources