Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 立即将tableView滚动到indexPath_Ios_Swift_Uitableview_Scroll - Fatal编程技术网

Ios 立即将tableView滚动到indexPath

Ios 立即将tableView滚动到indexPath,ios,swift,uitableview,scroll,Ios,Swift,Uitableview,Scroll,屏幕出现后,我必须立即将tableView滚动到indexPath。这是我尝试过的,但一开始就不起作用。只有在加载tableView后,我按下滚动tableView的按钮,它才会工作 func scrollTableViewToCell() { let indexPath = NSIndexPath(forItem: 8, inSection: 1) self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition

屏幕出现后,我必须立即将tableView滚动到indexPath。这是我尝试过的,但一开始就不起作用。只有在加载tableView后,我按下滚动tableView的按钮,它才会工作

func scrollTableViewToCell() {
    let indexPath = NSIndexPath(forItem: 8, inSection: 1)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Middle, animated: true)
}
我也尝试过这个,但它不能满足我的需要:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    println(indexPath)
    println(NSIndexPath(forRow: tableView.indexPathsForVisibleRows()!.last!.row, inSection: 2))
    if indexPath == NSIndexPath(forRow: tableView.indexPathsForVisibleRows()!.last!.row, inSection: 0) {
        scrollTableViewToCell()
    }
}
我也试过这个)

有人能告诉我如何知道tableView何时加载了所有单元格,然后将其滚动到NSIndexPath(forItem:8,inSection:1)

更新::

以下是我如何将数据加载到tableView:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return years.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return allInfo[years[section]]!.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("tutionTableViewCell") as! TuitionTableViewCell
    cell.selectionStyle = .None

    let info = allInfo[years[indexPath.section]]! as [String: String?]

    cell.monthLabel.text = months[indexPath.row]
    if info[months[indexPath.row]]! != nil {
        cell.tuitionLabel.hidden = false
        cell.tuitionLabel.text = info[months[indexPath.row]]!
    } else {
        cell.tuitionLabel.hidden = true
    }
    if info[months[indexPath.row]]! == "Nog te betalen" {
        cell.monthLabel.textColor = UIColor(hex: 0xff0000).colorWithAlphaComponent(0.87)
        cell.tuitionLabel.textColor = UIColor(hex: 0xff0000).colorWithAlphaComponent(0.87)
    } else {
        cell.monthLabel.textColor = UIColor.blackColor().colorWithAlphaComponent(0.87)
        cell.tuitionLabel.textColor = UIColor.blackColor().colorWithAlphaComponent(0.87)
    }
    return cell
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return years[section]
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.textLabel.font = UIFont(name: "Roboto-Regular", size: 16)
    header.textLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
    let headerFrame = header.frame
    header.textLabel.frame = headerFrame
    header.textLabel.textColor = UIColor.blackColor().colorWithAlphaComponent(0.54)
    header.textLabel.backgroundColor = UIColor(hex: 0xf9f9f9)
    header.contentView.backgroundColor = UIColor(hex: 0xf9f9f9)
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 35
}
第二次更新::

我的硬编码数据:

var years: [String] = ["2015", "2016", "2017"]
var months: [String] = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"]
var allInfo: [String: [String: String?]] = [String: [String: String?]]()

override func viewDidLoad() {
    self.tableView.separatorStyle = .None
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 150.0

    allInfo[years[0]] = ["Januari": "300,00", "Februari": "300,00", "Maart": "300,00", "April": "300,00", "Mei": "300,00", "Juni": "300,00", "Juli": "300,00", "Augustus": "300,00", "September": "300,00", "Oktober": "300,00", "November": "300,00", "December": "300,00"]
    allInfo[years[1]] = ["Januari": "300,00", "Februari": "300,00", "Maart": "300,00", "April": "300,00", "Mei": "300,00", "Juni": "300,00", "Juli": "300,00", "Augustus": "300,00", "September": "Nog te betalen", "Oktober": nil, "November": nil, "December": nil]
    allInfo[years[2]] = ["Januari": nil, "Februari": nil, "Maart": nil, "April": nil, "Mei": nil, "Juni": nil, "Juli": nil, "Augustus": nil, "September": nil, "Oktober": nil, "November": nil, "December": nil]

    tableView.reloadData()
    scrollTableViewToCell()
}

确保您已经实现了方法
EstimatedHeightForrowatineXpath
-

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 44
}
当您将tableview直接滚动到从未渲染过的单元格下方的行时,如果tableview不知道“缺少”单元格的大小,则无法正确布局单元格

在我的测试应用程序中实现这个方法解决了滚动问题和我在向上滚动时看到的奇怪的“轻弹”


另外,您应该在
viewDidLoad
方法中调用
super.viewDidLoad()

,非常感谢@Paulw11帮助我解决问题

我通过删除viewDidLoad中的这两行代码解决了这个问题:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 150.0
现在它可以根据需要滚动

此外,通过添加滚动动画消除了滚动滞后:

self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Middle, animated: true)

在viewdidload中设置计时器。这会解决你的问题

    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.scrollToRowMethod), userInfo: nil, repeats: false)

您是否尝试在
ViewDidDisplay
中滚动?您应该只在表格完全加载后调用
scrollTableViewToCell()
,例如在
[table reloadData]
@Fonix之后。我尝试了这个
viewDidLoad(){tableView.reloadData()scrollTableViewToCell()}
但是它滚动到了错误的位置。我想让我的tableView滚动到屏幕中央的indexPath,但它显示在顶部。@Fonix红色单元格应该在屏幕中央。奇怪。我用你的代码创建了一个简单的应用程序。当我运行应用程序时,它会正确地滚动到单元格,但当它创建单元格时,向上滚动是非常不平稳的。一旦您第一次滚动到顶部,则滚动将变得平滑。如果我不执行初始滚动,那么滚动也很好哦,我们同时也写了答案!)是的,我也发现了这个问题。非常感谢你的帮助!虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。当然。我添加了一段代码片段,这对我很有用。在调用ViewDidDisplay之前,我曾让它在viewDidLayoutSubviews中滚动,因为我不希望用户看到视图滚动,但这简化了逻辑,效果很好。
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.scrollToRowMethod), userInfo: nil, repeats: false)