Setting UITableView and section direction/alignment to be RTL (Right to Left) - ios

I created a UITableView in swift, I know I can change the cell direction like this:
cell.textLabel?.textAlignment = NSTextAlignment.Right
But how can I do it to the section? and/or to the entire table?

I followed #ReyGonzales comment and implemented the following tableView:viewForHeaderInSection:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var title: UILabel = UILabel()
title.text = "SomeText"
title.textAlignment = NSTextAlignment.Right
return title
}

Related

How to programmatically update a label based on the section of a UICollectionView

Say I have a horizontal UICollectionView with 3 sections. I want it to immediately update a label based on which section the user is in as they are scrolling. I found a few solutions that do this by using scrollViewDidEndDecelerating, but I want it to update the moment it goes from section 0 to section 1, from section 1 to section 2, etc.
Any thoughts?
there are
let indexPath = tableView.indexPathForSelectedRow();
var sectionNumber = indexPath.section
and check if 0 show update the first label and so on but i think best place in cell for row at index and get the section number then update it's label
there are another solution
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//Create label and autoresize it
let headerLabel = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 2000))
headerLabel.font = UIFont(name: "Avenir-Light", size: 30)
headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
//Adding Label to existing headerView
let headerView = UIView()
headerView.addSubview(headerLabel)
return headerView
}
also try this
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "Futura", size: 11)
header.textLabel?.textColor = UIColor.lightGrayColor()
}

Cell runs out of tableview iOS 10

I implement the table view on the right. And as u can see, the cells go out the table view. (It even runs all the way down to the bottom)
I have tried set cell cliptoBounds = true but doesn't work
var boxes = ["Box 1", "Box 2", "Box 3", "Box 4", "Box 5"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.historyTableViewOutlet.dequeueReusableCell(withIdentifier: "HistoryCell", for: indexPath) as! HistoryVC
cell.boxName.text = boxes[indexPath.row]
return cell
}
In the HistoryVC: `class HistoryVC: UITableViewCell {
//MARK: Outlets
#IBOutlet weak var placeholderView: UIView!
#IBOutlet weak var boxName: UILabel!
}
Is there anything wrong?
What you are trying to do is to create a UIView for section header.
First: make sure your tableView in the Main Storyboard is declared with type Plain.
Second: implement 2 methods of TableViewDelegate
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView(frame: CGRect(x: self.tableView.frame.minX, y: self.tableView.frame.minY, width: self.tableView.frame.size.width, height: 28))
let label = UILabel(frame: header.frame)
header.addSubview(label)
label.text = "SECTION HEADER"
label.textAlignment = .center
label.backgroundColor = UIColor(red: 0/255, green: 230/255, blue: 255/255, alpha: 0.9)
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 28
}
Sure you will want to customize that view for various sections. Just switch inside the delegate method between section numbers and assign views/text/labels/colors at your choice.
Once you scroll through cells within 1 section, its header will be glued to the top layout border.
PS After new details: you should put a constraint to clip the tableView to the top layout border. Cells do not run away from a tableView. They are scrolled up, beyond the visible zone, within the scrollView that contains the tableView. Just put exact constraints to fix the position of the tableView. Here:
Try to give your table's border some color so you can see the changing. Then try cliptobonds property of tableview not cell as you said in query.

Too long text for titleForHeaderInSection

So what my problem is that my text above my section is too long and gets cut off.
Any way to solve this like making it two rows long?
Any help is appreciated
You need to define the heightForHeaderInSection and customize viewForHeaderInSection. You can either fix all header heights at a value big enough for all lines, or calculate the required height for the specific header (as below).
let headerFont:UIFont = UIFont.systemFontOfSize(14);
let headerTexts = ["one line", "two line test123 sadfjklsadf asdjfklasjdflk asdfjklasdjfl asdfjklsadf"];
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return heightOfHeaderText(headerTexts[section]);
}
func heightOfHeaderText(text:String) -> CGFloat{
return NSString(string: text).boundingRectWithSize(
CGSizeMake(self.tableView.frame.size.width, 999),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName : headerFont],
context: nil).size.height;
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel:UILabel = UILabel.init(frame: CGRectMake(0, 0, tableView.frame.size.width, self.tableView(tableView, heightForHeaderInSection: section)));
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping;
headerLabel.font = headerFont;
headerLabel.text = headerTexts[section];
return headerLabel;
}
Make a custom view with a label in it. And use viewForHeaderInSection delegate method to assign text to that label and return this view.
EDIT:
See this link
Customize UITableView header section

UITableView HeaderView margin when removing separator's left space

I've removed the separator left margin from my tableview using in the viewDidLoad:
self.tableView.layoutMargins = UIEdgeInsetsZero
self.tableView.separatorInset = UIEdgeInsetsZero
In my cellForRowAtIndexPath:
cell.layoutMargins = UIEdgeInsetsZero
The problem is that the header and footer views has been moved too and I want to keep it aligned with the cells' content.
I've tried to change the frame in the willDisplayHeaderView and willDisplayFooterView without success. Any suggestion? Thanks!
As Memon Irshad suggest, I've created a new label but rather than do it inside the viewForHeaderInSection I've do it in willDisplayHeaderView because while using this method we can know the size of the headerView.
Then I hide the original textLabel of the TableViewHeader and adding the new framed label. What do you think? Any improvements?
Looks like this:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let originView = view as! UITableViewHeaderFooterView
let lbl = UILabel(frame: CGRectMake(15,originView.frame.height-30,originView.frame.size.width,20))
// Setting up the new label
lbl.numberOfLines = 0
lbl.font = Constants.Fonts.HeaderRow
lbl.textColor = Constants.Colors.TextColored
// Copying the original text content
lbl.text = originView.textLabel?.text
// Hidding the original label
originView.textLabel?.hidden = true
originView.addSubview(lbl)
}
Header/Footer cannot stick to your cell's content. I too wasted a lot of time on that
You must add a row to your section and use it as if it is a header/footer
try this code set custom label frame in header view
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
let lbl = UILabel(frame: CGRectMake(15,2,self.view.frame.size.width,20))
lbl.textColor = UIColor.whiteColor()
lbl.text = "SOPORTE"
lbl.font = UIFont(name: "Helvetica Neue", size: 15)
view.addSubview(lbl)
return view
}

Prevent footer overlapping tableViewCell in UITableView - Swift

I have this table view which works how i want it to however i have a problem where the footer overlap the cells in the table view as seen in
How can i prevent this? This is my code for the footer
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
// self.myTableView.tableFooterView = footerView;
let sectionString = Array(foodArray.keys)[section]
for value in caloriesArray[sectionString]! {
calories += value
}
totalCalories += calories
print(calories)
print(totalCalories)
let label = UILabel(frame: CGRectMake(footerView.frame.origin.x - 15, footerView.frame.origin.y, footerView.frame.size.width, 20))
label.textAlignment = NSTextAlignment.Right
label.text = "Total Calories: \(calories) "
footerView.addSubview(label)
calories = 0
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
#IBAction func addFoodTapped(sender: AnyObject) {
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let sectionString = Array(foodArray.keys)[indexPath.section]
foodArray[sectionString]?.removeAtIndex(indexPath.row)
caloriesArray[sectionString]?.removeAtIndex(indexPath.row)
print(foodArray)
viewDidAppear(true)
}
You can do that, Just make Style Grouped as shown below:
Just add some padding to the bottom of the content so that it doesn't overlap with the footer:
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, FOOTER_HEIGHT, 0)
I know this is an old thread, so this is more so for others that encountered this (like myself).
The floating section footer is default behavior from my understanding. There are a couple of options that I can think of:
Provide your footer view with a background color (footerView.backgroundColor = UIColor.white) thus cleaning up the overlap.
or
Replace the section footer with a custom "Total Calories" cell that you add to the table after the last cell in that section, effectively acting like a footer cell.
Sorry for the delay. If you have modified the standard contentInsetAdjustmentBehavior of the tableView, you must adjust the tableView contentInset property to take into account the total height of the views at the bottom of the UITableView, like the tab bar. If contentInsetAdjustmentBehavior is set to "automatic" (or you didn't change the default value), then set the clipsToBounds property of your footer view to true so that its child views cannot be painted outside the footer view layer's frame. That should solve your issue.
Try to override this method
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 44.0
}
Just set your label's background color to UIColor.white. and you are done !
label.backgroundColor = UIColor.white
Of course it overlaps. This is how footers and header work in UITableViews. You can set the footerView.backgroundColor to UIColor.gray, for example, to make it look better.

Resources