2016-08-28 16 views
2

UITableViewを最後のセルにスクロールするにはどうすればよいですか?UITableViewを下にスクロールして開始

  • は、ビューが登場していない後
  • をアニメーションではない
  • テーブルビューであってもサブビューとして追加されていない後

だけで画面上に提示したときに、スクロール開始すること平野UITableView(frame: CGRectZero, style: UITableViewStyle.Plain)を持っているすべての一番下まで

は、私はまた、古典的なCGAffineTransformMakeScale(1,-1)ハックを試した(私のサブクラスで)のtableViewのinit方法に

// 1 
reloadData() 
scrollToRowAtIndexPath(
    NSIndexPath(forItem: dataArray.count-1, inSection: 0), 
    atScrollPosition: .Top, animated: false 
) 

// 2 
reloadData() 
var contentOffset = self.contentOffset 
contentOffset.y = CGFloat.max 
setContentOffset(contentOffset, animated: false) 

を、それは私の細胞が底に固執して行います

私が試してみました私は彼らがトップをつけたかった(しかし、ボトムにスクロールした)。 (セル数が少なく、全体がUITableViewのスペースを占めていない場合にのみ関連します)

EDIT:別の詳細は、私はダイナミックセルUITableViewAutomaticDimensionを使用しています。

+0

についてhttps://developer.apple.com/library/ios/documentation/UIKit/Referenceを参照してください/UIScrollView_Class/index.html#//apple_ref/occ/instm/UIScrollView/setContentOffset:animated:私はそれがあなたが探しているものだと思います。 animatedをfalseに設定するだけです。 – Kendel

+0

最初のものについては、atScrollPositionを.Bottomに設定してみてください。 – penatheboss

+0

'viewDidLoad'に' tableView.setContentOffset(CGPointMake(0、CGFloat.max)、animated:false) 'を呼び出すだけです。 – ozgur

答えて

0

EDIT:アニメーション

:偽

func scrollBottom() { 
    let lastIndex = NSIndexPath(forRow: dataArray.count-1, inSection: 0) 
    self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false) 
} 

テストコード:

import UIKit 

class TableViewController: UITableViewController { 

var goButton = UIButton() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    goButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) 
    goButton.backgroundColor = UIColor.blueColor() 
    goButton.addTarget(self, action: #selector(TableViewController.scrollBottom), forControlEvents: .TouchUpInside) 
    self.view.addSubview(goButton) 
} 



override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 500 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    cell.textLabel?.text = "Hello, World!" 

    return cell 
} 

func scrollBottom() { 
    let lastIndex = NSIndexPath(forRow: 499, inSection: 0) 
    self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false) 
} 

} 
+0

私は私の質問で言ったように、私はそれがアニメ化されることを望んでいません。 –

+0

アニメーションをfalseに変更すると、 – aatalyk

+0

が最初の試行になります... –

1
これは、任意のグリッチなしで一番下までスクロールします

が、あなたの場合Tableviewを使用するロールツー行のプロパティを使用すると、グリッチが発生します。

スウィフト3使用するため

self.TableView.reloadData() // To populate your tableview first 
let bottomOffset = CGPoint(x: 0, y: self.TableView.contentSize.height - self.TableView.frame.size.height) 
self.TableView.setContentOffset(bottomOffset, animated: false) 

オブジェクティブC使用

[YourTableView reloadData]; // To populate your tableview first 

[YourTableView setContentOffset:CGPointMake(0, YourTableView.contentSize.height - YourTableView.frame.size.height)]; 
+0

おそらくswift: - :[YourTableView setContentOffset:CGPointMake 0、YourTableView.contentSize.height - YourTableView.frame.size.height)animated:NO]; –

+0

もう一度、私の2回目の試みと同じです... –

関連する問題