IOS Problem Getting a pointer to a path at a specific point

I have a view controller that contains a navigation bar, a tabular view and a toolbar. I included the UITableViewDelegate in the view controller and correctly assigned the table data source and passed the view controller through the storyboard. The table view loads its data from the remote database, as soon as the table scrolls to the last cell, more data is loaded into the table. I achieved this using the scrollViewDidScroll and indexPathForRowAtPoint methods, as described in the following post: How to find out when the UITableView scrolled to the end on the iPhone . However, when I launch the application and look at the table, the only index path returned by indexPathForRowAtPoint is the one that was located at the specified point while the table was loading. Here is the code and the output that I get when scrolling:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint bottomPoint = CGPointMake(160, 430); CGPoint topPoint = CGPointMake(160, 10); NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]); } 

Each scroll displays the following:

 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 index cell of the cell, the bottom point is on when the controller boots up. What am I doing wrong and why is this happening? Does this have anything to do with the fact that the UITableView is inside the parent controller?

+5
source share
4 answers

bottomPoint looking for a location inside your scrollView . Your scrollView contains a table and all cells are in the PLACE all the time relative to your scrollView . That is why you always get the same cell at that moment.

When you scroll, cells do not move in scrollView , scrollView "moves" relative to its parent. This is done by changing its contentOffset.

If you add scrollView y content scrollView to your bottomPoint , you get what you are probably really looking for in your scrollView .

like this:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint bottomPoint = CGPointMake(160, 430) - scrollView.contentOffset.y; CGPoint topPoint = CGPointMake(160, 10); NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]); } 
+9
source

As you can see from the Apple documentation, the indexPathForRowAtPoint: method indexPathForRowAtPoint: refers to a point in the local coordinate system of the receiver ( table view border ), BUT BUT the table view frame. Since the lower limit that you set is in the coordinate system of the table frame, you will not get indexPath right. HalR's answer gives bottomPoint in the coordinate system of the table borders.

indexPathForRowAtPoint:

Returns a pointer path defining a string and section at a given point.

 - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point 

Options
point: point in the local coordinate system of the receiver (table border).

Return value
An index path representing the row and section associated with the dot, or nil if the dot is outside the bounds of any row.

FYI between frame and borders

+3
source

Here is a class to implement this function.

import UIKit

class ViewController: 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))") } } 

}

0
source
 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) 

This should work too, and it may be easier to understand. All additional spaces (for example, the bottom insert) will be handled differently for each case.

0
source

Source: https://habr.com/ru/post/1485146/


All Articles