6

I have a view controller that contains a navigation bar, a table view and a tool bar. I included the UITableViewDelegate in the view controller, and correctly assigned the table's data source and delegate to the view controller through the storyboard. The table view loads its data from a remote database, once the table scrolls to the last cell more data is loaded into the table. I achieved this by using the scrollViewDidScroll and indexPathForRowAtPoint methods as outlined in the following post: How to know when UITableView did scroll to bottom in iPhone. However, when I run the app and scroll through the table the only index path returned by indexPathForRowAtPoint is the one that was located at the specified point at the time of table load. Here is the code and the output I get when I scroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint bottomPoint = CGPointMake(160, 430);
    CGPoint topPoint = CGPointMake(160, 10);

    NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]);

}

Every time I scroll the following is outputted:

2013-06-08 00:56:45.006 Coffee[24493:907] 3
2013-06-08 00:56:45.012 Coffee[24493:907] 3
2013-06-08 00:56:45.040 Coffee[24493:907] 3
2013-06-08 00:56:45.069 Coffee[24493:907] 3
2013-06-08 00:56:45.088 Coffee[24493:907] 3
2013-06-08 00:56:45.105 Coffee[24493:907] 3
2013-06-08 00:56:45.135 Coffee[24493:907] 3
2013-06-08 00:56:45.144 Coffee[24493:907] 3
2013-06-08 00:56:45.173 Coffee[24493:907] 3
2013-06-08 00:56:45.180 Coffee[24493:907] 3

Where 3 is the indexPath.row of the cell the bottom point is on when the controller loads. What am I doing wrong and why is this happening? Does it have anything to do with the fact that the UITableView is located inside a parent view controller?

4

4 回答 4

11

bottomPoint正在寻找您的scrollView. 您scrollView包含一个表格,相对于您的scrollView. 这就是为什么你总是在那个时候得到同一个单元格。

滚动时,单元格不会在 中移动,而是scrollViewscrollView对于其父级“移动”。这是通过更改其 contentOffset 来完成的。

如果您将您scrollView的 'sy 内容偏移量添加到您bottomPointscrollView.

像这样:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint bottomPoint = CGPointMake(160, 430) - scrollView.contentOffset.y;
    CGPoint topPoint = CGPointMake(160, 10);

    NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]);

}
于 2013-06-08T05:21:28.700 回答
3

正如您从 Apple 的文档中看到的那样,该方法indexPathForRowAtPoint:是指接收器的本地坐标系中的一个点(表格视图的边界),不是表格视图的框架。由于您设置的 bottomPoint 在表格视图的框架坐标系中,因此您不会得到正确的indexPath. HalR 的答案给出了表格视图边界坐标系中的底部点。

indexPathForRowAtPoint:

返回标识给定点的行和节的索引路径。

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

参数
point: 接收者本地坐标系中的一个点(表格视图的边界)。

返回值表示与点关联的行和部分
索引路径nil,或者该点是否超出任何行的范围。

仅供参考Frame 和 Bounds 之间的差异

于 2013-06-08T07:27:56.607 回答
1

这是实现该功能的类。

导入 UIKit

类视图控制器:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    let flagView = UIView(frame: CGRect(x: 0, y: 100.0, width: self.view.frame.width, height: 20.0))

        flagView.backgroundColor = .blue

        self.view.addSubview(flagView)

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = UITableViewCell()
    cell.textLabel?.text = "Cell number: \(indexPath.row)"
    return cell
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y > 0 {
        let currentPoint = CGPoint(x: self.view.frame.width - 100, y: 100.0 + scrollView.contentOffset.y)
        print(" current index:  \(String(describing: tableView.indexPathForRow(at: currentPoint)?.row))")
    }
}

}

于 2017-11-09T09:16:16.527 回答
0
let bottomPoint = CGPoint(x: tableView.frame.midX, y: tableView.frame.maxY)
let converted = tableView.convert(point, from: tableView.superview)
let indexPathForBottomCell = tableView.indexPathForItem(at: converted)

这也应该有效,并且可能更容易理解。对于每种情况,所有额外的空格(如底部插图)都将以不同的方式处理。

于 2018-12-19T15:39:05.773 回答