当用户从detailViewController返回到UITableView时,我希望tableView返回到先前的位置。找到this post的解决方案,但由于我没有保存选定的行,因此无效。这就是我被困住的地方。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Calculator</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"></head>
<body>
<div id="calculatorcontainer">
<div id="maincontainer">
<table>
<tr>
<td>
<form action="" method="post" id="theForm">
<fieldset >
<legend>Calculator</legend>
<h4>Use this form to calculate the order total</h4>
<div class="form-group">
<label for="quantity">Quantity</label>
<input class="form-control" id="quantity" type="number" value="1"/>
</div>
<div class="form-group">
<label for="ppu">Price Per Unit</label>
<input type="text" class="form-control" id="ppu" placeholder="1.00" required>
</div>
<div class="form-group">
<label for="tax">Tax Rate (%)</label>
<input type="text" class="form-control" id="tax" placeholder="0.0" required>
</div>
<div class="form-group">
<label for="discount">Discount (%)</label>
<input type="text" class="form-control" id="discount" placeholder="0.00" required>
</div>
<div class="form-group">
<label for="ttl">Total</label>
<input type="text" class="form-control" id="output" placeholder="0.00">
</div>
<div>
<button type="submit" class="btn btn-calculate" id="submit">Calculate</button>
</div>
</fieldset>
</form>
</td>
<td>
<aside class="imageContainer">
<img src="https://image.ibb.co/iUhiUv/Screen_Shot_2017_08_03_at_12_12_16_PM.png">
</aside>
</td>
</tr>
</table>
</div><!-- End "maincontainer" -->
<footer class="footer">
<h3>Copyright CSUN</h3>
</footer>
</div>
</body>
</html>
带
func scrollToSelectedRow() {
let selectedRows = self.tableView.indexPathsForSelectedRows
if let selectedRow = selectedRows?[0] as NSIndexPath? {
self.tableView.scrollToRow(at: selectedRow as IndexPath, at: .middle, animated: true)
}
}
在viewDidAppear中。
我保存下面的行,将详细信息发送到DetailViewController
scrollToSelectedRow()
但是,不确定如何将两者联系在一起。
任何帮助表示赞赏
答案 0 :(得分:1)
您的代码存在问题
let selectedRows = self.tableView.indexPathsForSelectedRows
if let selectedRow = selectedRows?[0] as NSIndexPath? {
self.tableView.scrollToRow(at: selectedRow as IndexPath, at: .middle, animated: true)
}
如果从未调用过语句
尝试这样做
像这样声明你的索引路径
var selected = IndexPath(row: NSNotFound, section: NSNotFound)
现在在didselect类
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ChatViewController")
self.present(nextViewController, animated:true, completion:nil)
self.selected = (indexPath as NSIndexPath) as IndexPath
}
调用此函数
func scrollToSelectedRow() {
if self.selected.row != NSNotFound
{
self.tableView.scrollToRow(at: self.selected as IndexPath, at: .middle, animated: true)
}
}
在
override func viewWillAppear(_ animated: Bool) {
scrollToSelectedRow()
}