我目前正在构建一个聊天应用,遇到了问题。每当我尝试根据向谁发送消息来向左或向右对齐UILabel时,它将不起作用。每次收到消息时,我都会重新加载TableView,但标签不会被更新。我正在使用swift 2.0。我还能尝试什么?还是有另一种方法可以做得更好?
//in cellForRowAtIndexPath
if message == my own {
//tried this
cell.nameOfSender.frame.origin.y = 0
cell.nameOfSender.frame.origin.x = widthOfScreen
//And this
cell.nameOfSender.center = CGPointMake(95,15)
} else{
cell.nameOfSender.frame.origin.y = 0
cell.nameOfSender.frame.origin.x = 0
//And this
cell.nameOfSender.center = CGPointMake(95,15)
}
最佳答案
您有两种方法,
首先是使用NSTextAlignment,因此您可以执行以下操作:
cell.lblTitle.textAlignment = .Left
第二个是为每种样式(左和右)创建不同的UITableViewCell子类
例如:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
//ChatCell is a Subclass of UITableViewCell
var cell:ChatCell
// Just Random presenter getting the chat for the row
let chat = presenter?.getChat(row)
//Getting info whether the chat sender is me or not and the instances of each custom cells.
if chat.sender == ChatSender.Me {
cell = ChatCellSender(title: "Example")
}else{
cell = ChatCellReceiver(title: "Example")
}
return cell
}