按钮在一个滚动型的TableView不会触发
问题描述:
我有一个滚动型2个tableViews:按钮在一个滚动型的TableView不会触发
- 的tableViews不能滚动,将显示所有行
- 两个tableViews有按钮 定制单元
- 按从TableView2一个按钮添加一行TableView1
问题:
1)它最初工作时,按下TableView2中的按钮将执行服务调用,然后向TableView1中添加一个单元格。
2)但是我注意到,一旦TableView1展开占据屏幕的高度,我向下滚动访问TableView2,按下TableView2上的按钮不再注册任何东西。它根本不会触发该动作。这是我认为可能是问题的想法,但我不确定。
一些代码
extension MyFeedViewController: UITableViewDelegate,UITableViewDataSource{
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableView == self.tableView1
{
return tableView1Array.count
}
else
{
return tableView2Array.count
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if tableView == self.tableView1
{
let cell:TableView1Cell = self.tableView1.dequeueReusableCell(withIdentifier: "TableView1Cell", for: indexPath) as! cell:TableView1Cell
cell.updateCell(withObject:)
cell.delegate = self
return cell
}
else
{
let cell:TableView2Cell = self.tableView2.dequeueReusableCell(withIdentifier: "TableView2Cell", for: indexPath) as! TableView2Cell
cell.updateCell(withObject:withType)
cell.delegate = self
return cell
}
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if tableView == self.tableView1
{
return CGFloat(80)
}
else
{
return CGFloat(60)
}
}
}
一旦从服务器获取成功响应,我触发我的滚动视图的重新计算是这样的:
override func viewDidLayoutSubviews()
{
refreshTableHeights()
}
//Called when a success server call is made
func refreshTableHeights()
{
//Update table height
if tableView1Array.count > 0
{
tableView1HeightConstraint.constant = CGFloat(tableView1Array.count * 80)
}
else
{
tableView1HeightConstraint.constant = CGFloat(80)
}
if tableView2Array.count > 0
{
tableView2HeightConstraint.constant = CGFloat(tableView2Array.count * 60)
}
else
{
tableView2HeightConstraint.constant = CGFloat(60)
}
view.layoutIfNeeded()
//Set content view
let extraSpace = CGFloat(200)
contentView.frame.size.height = tableView1.frame.size.height + tableView2.frame.size.height + extraSpace
scrollView.contentSize = contentView.frame.size
}
一些其他的注意事项:
- 桌面高度固定在我的IB,所以我手动更改tableViews的高度约束的代码。
-
带有其IBActions的按钮在其自定义单元中定义。按下TableView 2中的一个按钮将触发代表回拨给我的VC进行服务呼叫。
如果有人能指引我走向正确的方向,我会迷失方向,那将会很棒。谢谢!
答
通过在模拟器中运行它来捕获您的接口。 查看按钮是否位于其超视图的框架中,如果视图不在其超视图范围内,则视图上的触摸操作不响应。
你能更具体地说明你为什么手动更新表视图高度吗?另外,看看您的自定义单元类看起来像 – Alex
在模拟器中运行它会捕获您的界面会很有帮助。看到它的按钮是否在其超视图的框架中。如果视图不在其超级视图之外,则视图上的触摸操作不会响应。 – Naresh
@Naresh谢谢你,那是我的问题。我对contentview有一个约束,它在滚动视图滚动时重置contentview的高度。 – Moo33