Swift:在 tableview 中插入新行时出现滚动问题 [英] Swift: Scrolling issue while inserting new row in tableview
问题描述
我在做聊天应用.我对
也许这个对你有帮助
func tableView(tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{退货}func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) ->UITableViewCell{让 cellId:String = "Cell"+String(indexPath.row)var cell = tableView.dequeueReusableCellWithIdentifier(cellId)如果(单元格 == 零){cell = UITableViewCell(样式:.默认,reuseIdentifier:cellId)}让 cellText:String = String(indexPath.row)(cell! as UITableViewCell).textLabel!.text = cellText返回单元格!作为 UITableViewCell}func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->CGFloat {返回 UITableViewAutomaticDimension}func tableView(tableView: UITableView,estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) ->CGFloat {返回 UITableViewAutomaticDimension}@IBAction func addRow(sender: UIButton) {self.inte = self.inte + 1self.myTblView.beginUpdates()self.myTblView.insertRowsAtIndexPaths([NSIndexPath(forRow: inte - 1, inSection: 0)], withRowAnimation: .Automatic)self.myTblView.endUpdates()self.scrollToBottom()//这里,TABLEVIEW 从顶部落下.}功能滚动到底部(){让 numberOfSections = myTblView.numberOfSections让 numberOfRows = myTblView.numberOfRowsInSection(numberOfSections-1)如果 numberOfRows >0 {打印(节数)打印(行数)让 indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))打印(索引路径)myTblView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, 动画: false)}}
I am doing chat app. I am having doubt in Unable to set cell size according to label's text size in objective-c .I have solved that issue.
Here, If I send new message, I have to insert new row as last row in tableview. I have tried with following code. But, tableview getting reload again while scrolling to last. UI is not seems good. Because, tableview falling from top. How to stop this? I am using auto layout. Gave constraints to tblCellLabel (UILabel - Customized from Story board) which is in UITableViewCell. I just want to insert a row in last without UI issue.
Kindly guide me how to solve this issue.
My Code:
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-Subclass Code
override func drawTextInRect(rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
override func intrinsicContentSize() -> CGSize {
let insets: UIEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
var intrinsicSuperViewContentSize = super.intrinsicContentSize()
intrinsicSuperViewContentSize.height += insets.top + insets.bottom
intrinsicSuperViewContentSize.width += insets.left + insets.right
return intrinsicSuperViewContentSize
}
My Output Screen Shot:
May be this one helps you
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)
}
}
这篇关于Swift:在 tableview 中插入新行时出现滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!