2016-05-17 83 views
1

我的swift APP有問題。Swift:如何居中細胞文本

在我TableViewCell功能我寫

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

如果我寫

cell.textAlignment red line appear and say "textAlignment in unavailable: API deprecated" 
+0

大概你在你的單元格上有一個標籤,並且你想對齊標籤中的文本,而不是整個單元格... – Wain

+0

您是否閱讀過'UITableViewCell'的textAligment文檔:「Deprecation Statement instead instead分配給textLabel和detailTextLabel屬性的UILabel對象的文本對齊。「將這個對齊應用到想要的'UILabel',而不是單元格。 – Larme

+0

我沒讀過。感謝您的意見。我解決它。 –

回答

8

使用雨燕4人正確的答案應該是:

cell.textLabel?.textAlignment = .center 
+0

我該怎麼做纔是有一個圖像和一個單元格中的標籤,我該如何居中? –

+1

您必須先將圖片置於居中位置,然後在您的圖片居中放入標籤後添加限制 – Thomas

0

試試這個

MyLabel1.textAlignment = NSTextAlignment.Center 
0

我假設您在您的手機上使用了標籤? 確保標籤在單元格中居中,並給它一個足夠大的寬度以滿足您的需求。 然後你可以使用兩種方法,您可以使用代碼,對齊標籤:

label.textAlignment = .Center 

,或者你可以在此使用界面生成器改變:

Alignment example in interface builder

希望這有助於!