4

I have a problem with my swift APP.

In my TableViewCell function i wrote

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as MyCell
cell.setMyCell(self.data[0]["content"]!) 

if i wrote

cell.textAlignment red line appear and say "textAlignment in unavailable: API deprecated"
Antonello Rossi
  • 41
  • 1
  • 2
  • 6
  • presumably you have a label on your cell and you want to align the text in the label, not the whole cell... – Wain May 17 '16 at 09:21
  • Did you read the documentation on `textAligment` for `UITableViewCell`: "Deprecation Statement Instead set the text alignment of the UILabel objects assigned to the textLabel and detailTextLabel properties." Apply the alignement to the wanted `UILabel`, not the cell. – Larme May 17 '16 at 09:30
  • N i didn't read. Thanks for your advise. I resolve it. – Antonello Rossi May 17 '16 at 09:59
  • @AntonelloRossi: Often, when a method is deprecated, the doc says what new method to use if available. If not, there are often on SO questions about the issue. – Larme May 17 '16 at 15:02

4 Answers4

14

The correct answer for people using Swift 4 would be:

cell.textLabel?.textAlignment = .center
Kevin Singh
  • 421
  • 8
  • 14
Thomas
  • 1,289
  • 1
  • 17
  • 29
4

It will work only when your UITableViewCell Style is default

Swift 4.1

cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "DefaultCell")
cell.textLabel?.textAlignment = .center
Sunil Targe
  • 7,251
  • 5
  • 49
  • 80
0

Try this

 MyLabel1.textAlignment = NSTextAlignment.Center
IOS Singh
  • 617
  • 6
  • 15
  • `'textAlignment' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift` – velkoon Dec 06 '18 at 21:11
0

I assume you are using a label on your cell? Make sure the label is centered in the cell and give it a width that is big enough for your needs. Then you can use two methods, you can align the label using the code:

label.textAlignment = .Center

or you can change this using the interface builder:

Alignment example in interface builder

Hope this helps!

nickyhvm
  • 51
  • 4
  • `'textAlignment' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift` – velkoon Dec 06 '18 at 21:12