![](/img/trans.png)
[英]How to change the width of a messageCell depending on the length of the message? in swift 4
[英]how to put messageCell at bottom of tableView? *Swift*
聊天設置。 messagetableView底部約束附加到dockView的頂部。 不幸的是,tableView messageCell不符合對tableView施加的相同約束。 在我的屏幕截圖中,您可以看到黃色的tableView。 它從superView的頂部開始,一直延伸到UIViewDock的頂部。 白色messageCell不遵循tableView。 顯然messageMessage之所以這樣做是因為只有5行消息,所以如何像聊天應用程序一樣使白單元格從tableView的底部開始?
另一個舊線程中的某個人說要使用此Obj-C。
@property (nonatomic, assign) BOOL shouldScrollToLastRow;
- (void)viewDidLoad
{
[super viewDidLoad];
_shouldScrollToLastRow = YES;
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Scroll table view to the last row
if (_shouldScrollToLastRow)
{
_shouldScrollToLastRow = NO;
[self.tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
}
}
這是正確的,我將如何在Swift中進行呢?
將您的messageCell設置為表格視圖的頁腳,使其始終固定在表格底部。 您可以在IB中的表中添加視圖,並為其提供IBOutlet(我在下面的代碼中稱為我的頁腳)。
@IBOutlet weak var footer: UIView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 44
tableView.tableFooterView = footer
tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentSize.height), animated: false)
}
在“發送”按鈕的操作方法中,您可以執行此操作以添加新行並使表格滾動到底部,
@IBAction func sendMessage(sender: UIButton) {
var message = messageField.text
messages.append(message)
let lastPath = NSIndexPath(forRow: messages.count - 1, inSection: 0)
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([lastPath], withRowAnimation: .Automatic)
tableView.contentSize = CGSize(width: tableView.contentSize.width, height: tableView.contentSize.height + tableView.rowHeight)
tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentSize.height), animated: true)
tableView.endUpdates()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.