【问题标题】:How to insert a new Row in bottom of the UITableView Swift如何在 UITableView Swift 底部插入新行
【发布时间】:2018-10-10 02:02:48
【问题描述】:

我用

func insertRowsAtIndexPaths(indexPaths: [NSIndexPath],
withRowAnimation animation: UITableViewRowAnimation)

在表格中插入新行的方法。但它出现在UITableView 的顶部(默认情况下)。

我找不到从表格底部插入行的任何选项,如屏幕截图所示:

我尝试使用UIEdgeInsetsMake

self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0)

但是当我滚动表格时它破坏了所有设计。

我的问题是:如何从表格底部插入新行?

UPD:当添加新行时,它应该看起来像表格从键盘视图移动到导航栏。

UPD2:这是我想要的:http://recordit.co/JoR7xuvvpG

我在

的帮助下做到了
self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0)

但是 contentInset 在行上方添加了一个大的白色字段,当我向下滚动时,它会隐藏添加的行。看起来他们隐藏在keyboardView下。我想阻止它。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    斯威夫特 3

        TableView.beginUpdates()
    
        let indexPath:IndexPath = IndexPath(row:(self.yourArray.count - 1), section:0)
    
        TableView.insertRows(at: [indexPath], with: .left)
    
        TableView.endUpdates()
    
        TableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    

    【讨论】:

      【解决方案2】:
      func insertRowsAtIndexPaths(indexPaths: [NSIndexPath],
      withRowAnimation animation: UITableViewRowAnimation)
      

      这是追加行的方法。要在底部插入行,您需要提供最后一行的索引路径。

      例如:

      var IndexPathOfLastRow = NSIndexPath(forRow: self.array.count - 1, inSection: 0)
      self.tableView.insertRowsAtIndexPaths([IndexPathOfLastRow], withRowAnimation: UITableViewRowAnimation.Left)
      

      【讨论】:

      • 感谢您的建议,但这并没有解决我的问题。对不起我的英语,也许我描述错了。看,我想要什么:recordit.co/JoR7xuvvpG 我在以下帮助下做到了:self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0) 但是当我向下滚动时,你可以看到大的白色字段在添加的行上方。我想阻止它。
      【解决方案3】:

      您是否尝试使用UITableViewRowAnimationBottom 作为行动画参数?
      像这样:(迅速)

      tableView.insertRowsAtIndexPaths([indexPath],   
          withRowAnimation: UITableViewRowAnimation.Bottom)
      

      或 Objective-C:

      [self insertRowsAtIndexPaths:indexPaths
                  withRowAnimation:UITableViewRowAnimationBottom];
      

      【讨论】:

      • 它不起作用,因为它只用于动画。行从 UITableView 的顶部出现,带有 .Bottom 动画。我希望行应该从表格底部出现。它应该看起来像从键盘出现的表格,并在添加新行时移动到顶部。
      • 那么,请确保添加到数据源的“底部”,并且新行的索引比行上最后一项的索引高一个。跨度>
      • 不幸的是,不起作用。我想这样做:recordit.co/JoR7xuvvpG
      • 那么你需要做的是,当你插入元素时,在它的父视图中为整个 UITableView 设置动画。如果您使用自动布局,请在属性中捕获 Top 约束,并在插入元素时将其更改为常量。类似于:topConstraint.constant = newValue; UIView.animateWithDuration(0.5, 动画: { self.layoutIfNeeded() })
      • 看视频,你不能乱用 contentInsets 来向上移动单元格项目吗?
      【解决方案4】:

      感谢@raf 的建议:

      然后,您需要做的是在插入元素时在其父视图中为整个 UITableView 设置动画。如果您使用autolayout,请在属性中捕获Top 约束,并在插入元素时将其更改为constant。类似的东西:

       topConstraint.constant = newValue
       UIView.animateWithDuration(0.5, animations: { 
        self.layoutIfNeeded() 
      })
      

      【讨论】:

      • 你能给我这段代码的 Objective-C 等价物吗?
      • 不幸的是没有:(我只能斯威夫特:)
      猜你喜欢
      • 2015-10-30
      • 2015-01-27
      • 1970-01-01
      • 2016-05-31
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多