想法是做一些像WhatsApp那樣爲每個用戶左右對齊消息的東西。在TableView行內動態對齊UITextView
我想這樣的代碼:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = messageTableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
// get the items in this section
let sectionItems = self.getSectionItems(indexPath.section)
// get the item for the row in this section
let currentMessage = sectionItems[indexPath.row]
var messageTextViewAlignment: NSLayoutFormatOptions
if (currentMessage.from == self.currentUserId)
{
messageTextViewAlignment = NSLayoutFormatOptions.AlignAllRight
}
else
{
messageTextViewAlignment = NSLayoutFormatOptions.AlignAllLeft
}
var newConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[messageTextView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["messageTextView": cell.messageTextView])
cell.messageTextView.addConstraints(newConstraint)
return cell
}
IM想這太,但沒有工作: cell.messageTextView.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin cell.messageTextView.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin –
我沒有看到你將「messageTextViewAlignment」分配給單元格的位置? – anhtu
在字段選項,我試過但沒有工作太... –