Ios Swift:在tableview中插入新行时出现滚动问题
我正在做聊天应用。我有点怀疑。我已经解决了那个问题 在这里,如果我发送新消息,我必须插入新行作为tableview中的最后一行。我尝试了以下代码。但是,当滚动到最后一页时,tableview将再次重新加载。UI看起来不太好。因为,桌面视图是从上面掉下来的。如何阻止这一切?我使用自动布局。对tblCellLabel UILabel进行了约束-从UITableViewCell中的故事板自定义。我只想在最后插入一行,没有UI问题 请指导我如何解决这个问题 我的代码:Ios Swift:在tableview中插入新行时出现滚动问题,ios,swift,uitableview,Ios,Swift,Uitableview,我正在做聊天应用。我有点怀疑。我已经解决了那个问题 在这里,如果我发送新消息,我必须插入新行作为tableview中的最后一行。我尝试了以下代码。但是,当滚动到最后一页时,tableview将再次重新加载。UI看起来不太好。因为,桌面视图是从上面掉下来的。如何阻止这一切?我使用自动布局。对tblCellLabel UILabel进行了约束-从UITableViewCell中的故事板自定义。我只想在最后插入一行,没有UI问题 请指导我如何解决这个问题 我的代码: func tableView(ta
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellId:String = "Cell"+String(indexPath.row)
var cell = myTblView.dequeueReusableCellWithIdentifier(cellId)
if (cell == nil){
cell = tblCell(style: .Default, reuseIdentifier:cellId)
}
(cell as! tblCell).tblCellLabel.text = "hjgyrtjjjg12hjgyrtjjjg12hjgyrtjjjg12hjgyrtjjjg12hjgyrtjjjg12hjgyrtjjjg12"
return cell as! tblCell
}
@IBAction func addRow(sender: UIButton) {
self.inte = self.inte + 1
self.myTblView.beginUpdates()
self.myTblView.insertRowsAtIndexPaths([
NSIndexPath(forRow: inte - 1, inSection: 0)
], withRowAnimation: .Automatic)
self.myTblView.endUpdates()
self.scrollToBottom() //HERE, TABLEVIEW FALLING FROM TOP.
}
func scrollToBottom()
{
let numberOfSections = myTblView.numberOfSections
let numberOfRows = myTblView.numberOfRowsInSection(numberOfSections-1)
if numberOfRows > 0 {
print(numberOfSections)
print(numberOfRows)
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
print(indexPath)
myTblView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
}
}
tblCellLabel子类代码
我的输出屏幕截图:
为什么要在scrollToBottom函数中使用行数-1这个可以帮助您吗
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return inte
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell{
let cellId:String = "Cell"+String(indexPath.row)
var cell = tableView.dequeueReusableCellWithIdentifier(cellId)
if (cell == nil){
cell = UITableViewCell(style: .Default, reuseIdentifier:cellId)
}
let cellText:String = String(indexPath.row)
(cell! as UITableViewCell).textLabel!.text = cellText
return cell! as UITableViewCell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
@IBAction func addRow(sender: UIButton) {
self.inte = self.inte + 1
self.myTblView.beginUpdates()
self.myTblView.insertRowsAtIndexPaths([
NSIndexPath(forRow: inte - 1, inSection: 0)
], withRowAnimation: .Automatic)
self.myTblView.endUpdates()
self.scrollToBottom() //HERE, TABLEVIEW FALLING FROM TOP.
}
func scrollToBottom()
{
let numberOfSections = myTblView.numberOfSections
let numberOfRows = myTblView.numberOfRowsInSection(numberOfSections-1)
if numberOfRows > 0 {
print(numberOfSections)
print(numberOfRows)
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
print(indexPath)
myTblView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
}
}
我不明白。若你们不想在更新后使用scrollToBottom,为什么还要使用它呢?我在做聊天应用程序。所以,最后一条消息应该在tableview中更新。所以,我在last中插入一行,tableview滚动到last。就这样,有解决办法吗?请把你的答案贴出来。我也面临着同样的问题。如果这是为了回答这个问题,请重写它,使其更加明确,因为反问句经常被误认为是不应该作为答案发布的澄清请求。我尝试了你的代码。工作很好。但是,我使用的是定制标签。检查我的问题。我更新了。请帮助我。@McDonal_11,我建议您,使用tableview单元格id应该是唯一的。我正在使用自定义tableview单元格。U r tryin用于默认tableViewCell。我已将UITableViewCell子类化为myTableCell,并且已将UILabel添加到该类中。请检查我的CellForRowatineXpath。在那里,我对tableViewCell进行了子类化。默认情况下,UR代码运行良好。如何定制tableViewCell?请引导我…我不明白myTablecell,请详细分享
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return inte
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell{
let cellId:String = "Cell"+String(indexPath.row)
var cell = tableView.dequeueReusableCellWithIdentifier(cellId)
if (cell == nil){
cell = UITableViewCell(style: .Default, reuseIdentifier:cellId)
}
let cellText:String = String(indexPath.row)
(cell! as UITableViewCell).textLabel!.text = cellText
return cell! as UITableViewCell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
@IBAction func addRow(sender: UIButton) {
self.inte = self.inte + 1
self.myTblView.beginUpdates()
self.myTblView.insertRowsAtIndexPaths([
NSIndexPath(forRow: inte - 1, inSection: 0)
], withRowAnimation: .Automatic)
self.myTblView.endUpdates()
self.scrollToBottom() //HERE, TABLEVIEW FALLING FROM TOP.
}
func scrollToBottom()
{
let numberOfSections = myTblView.numberOfSections
let numberOfRows = myTblView.numberOfRowsInSection(numberOfSections-1)
if numberOfRows > 0 {
print(numberOfSections)
print(numberOfRows)
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
print(indexPath)
myTblView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
}
}