Swift: Scrolling issue while inserting new row in tableview

╄→гoц情女王★ 提交于 2020-01-17 04:17:27

问题


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:


回答1:


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)
    }
}



回答2:


why are you using number of rows - 1 in scrollToBottom func



来源:https://stackoverflow.com/questions/34547969/swift-scrolling-issue-while-inserting-new-row-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!