I develop an iOS application and my native language is Persian (a Right-to-Left language). How can I change UITableView direction to right-to-left in iOS programming?
A UITableView with RTL direction has:
Icons are at right
TableCell detail and button are at left
TableView header and footer are right-aligned
like this :
Is there any UI or programming way to do this?
Try this on Swift
tableView.semanticContentAttribute = .forceRightToLeft
or in StoryBoard Cell properties (Semantic dropdown) select "Force Right-To-Left"
As I'm aware it'll automatically change to Right-to-Left since iOS8.
You can try to do this as well
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.textAlignment = .Right
cell.textLabel?.text = goalsArray[indexPath.row]
return cell
}
Build your view's constraints with NSLayoutAttributeLeading and NSLayoutAttributeTrailing instead of NSLayoutAttributeLeft and NSLayoutAttributeRight.
Your view's Direction will be the same with your app's language Direction.
for me this way worked
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)! as UITableViewCell
//cell.myView.backgroundColor = self.colors[indexPath.row]
cell.textLabel?.text =
"\((self.FAQs?[indexPath.row].Question)!)"
cell.textLabel?.textAlignment = .right
return cell
}
Related
I use dynamic height cells for my UITableView with this code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionsCell", for: indexPath) as! QuestionsCell
cell.label1.text = ""
cell.label2.text = ""
cell.label1.text = some text from array
cell.label2.text = some text from array
cell.label1.lineBreakMode = .byWordWrapping
cell.label1.numberOfLines = 0
cell.label1.sizeToFit()
return cell
}
Then I've pinned all constraints in Storyboard for each element (top, bottom, leading and trailing). On iPhone everything works fine, but on iPad when I scroll the UITableView this is happening (dynamic label height):
sizeToFit() only makes cell1 as big as it needs to be for the text.
You also need to use automatic height sizing for the table view cells.
In viewDidLoad,
self.tableView.rowHeight = UITableViewAutomaticDimension
You will also want to make sure you have a vertical constraint from your bold label to the "12 days ago" label, to ensure there is always vertical space between them, so they don't overlap.
you need to implement this delegate method:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
if this does not work then check if there is vertical spacing constraints between top textView and bottom textView.
how to add a right detail to a cell that already have left detail and subtitle. and how to give a textLabel to the right detail?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
return cell
}
Try this
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
let label = UILabel.init(frame: CGRect(x:0,y:0,width:100,height:20))
label.text = monster.name
cell.accessoryView = label
return cell
}
Click on your tableView cell. It's easier to do it in document outline.
Change the style attribute in Attribute Inspector from Basic to Custom. You will now be able to add a label. The basic template is set, so you can't alter it. I believe this was the issue you were having.
I'm sure after you place the label you know what to do, but just in case:
Place the new label at the right margin of the cell and pin it to the top, bottom, and right, using Autolayout.
Reconnect your title, subtitle IBOutlets, and create a right label IBOutlet, connect it, and you should be done.
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "approve_cell");
let event = self.events[indexPath.row];
cell.textLabel?.text = event.name;
return cell;
}
I placed 2 images and a label in tableviewcell. When i run the app, images are not visible. What could be the reason?
Check that you are not using the basic style of the UITableViewCell because if you are then that style only includes labels.. no image views. Change it to subtitle or one of the details.
For reference, see: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
You probably forgot to set AutoLayoutConstraints of your image view. You could either add constraints to that image view through storyboard or through code. The other method is setting the frame of your image view in -(void)viewDidLayoutSubviews, because when you're using auto-layout, the layout of those views will be undefined if its constraints doesn't fulfill system's computing need. So you should either set right constraints or give a specific frame to those undefined views after all of the others has-constraints-views are arranged.
You creat cell in storyboard but you never use this cell created from storyboard. instead you manually creat it by UITableViewCell's designated initializer without override layout method of it.
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "approve_cell", for: indexPath)
let event = self.events[indexPath.row];
cell.textLabel?.text = event.name;
return cell;
}
You need to set either Autolayout or used Autoresizing. For autolayout just pinned the proper leading, trailing, top and bottom constraint.
I'm developing my first app, and have got everything working except I have discovered a display issue on iPad, where the separator of a cell is limited in width (see screenshot)
Screen shot on iPad
I've tried everything that I can find on how to set the margins to 0, etc,
including:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! MyCell
myCell.preservesSuperviewLayoutMargins = false
myCell.separatorInset = UIEdgeInsets.zero
myCell.layoutMargins = UIEdgeInsets.zero
return myCell
}
I'm not using storyboard and I am doing it programmatically,
Any ideas would be appreciated,
Thanks
I believe it's caused by Readable Content Guide. You can disable it by setting tableView.cellLayoutMarginsFollowReadableWidth = false
This is what my story board shows
However, the right arrow is not there when I run on simulator
I don't know why and I don't know what code I should show you because I actually didn't make that arrow myself, I just selected the cell's accessory as Discolosure Indicator
update 1
there is no overlap, this is from the debugger:
The problem is that you implemented layoutSubviews, but never called super.layoutSubviews(). Thus the disclosure indicator was never laid out.
Try adding this inside your tableview method
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
//Complete Code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
try adding disclosure indicator programmatically in cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
...
}
Try using preview in the assistant editor. Most likely one of the images is overlapping the indicator and you need to adjust your constraints.