如何在 UITableViewCell 中左对齐或右对齐 UILabel
How to align a UILabel left or right in a UITableViewCell
我目前正在构建一个聊天应用程序,但遇到了一个问题。每当我尝试根据发送消息的人向左或向右对齐 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
}
我目前正在构建一个聊天应用程序,但遇到了一个问题。每当我尝试根据发送消息的人向左或向右对齐 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
}