Swift:在tableview

时间:2015-12-31 14:46:22

标签: ios swift uitableview

我在做聊天应用。我怀疑Unable to set cell size according to label's text size in objective-c。我已经解决了这个问题。

在这里,如果我发送新消息,我必须在tableview中插入新行作为最后一行。我试过以下代码。但是,tableview在滚动到最后时再次重新加载。用户界面似乎并不好。因为,tableview从顶部落下。怎么阻止这个?我正在使用自动布局。给UITableViewCell中的 tblCellLabel(UILabel - 自定义故事板)提供约束。我只想在没有UI问题的情况下在最后插入一行。

请指导我如何解决这个问题。

我的代码:

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
    }

我的输出屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:0)

可能这个可以帮助你

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

答案 1 :(得分:-2)

为什么使用行数 - scrollToBottom func中的1