i have UITableview , many rows/cell in tableview.
when UITableview scrolling at that time , find which row/cell at center of the Screen.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.
There are a couple of options for getting the central row. One would be to get the indexPath at the centre:
NSIndexPath *indexPathAtCenter = [self.tableView indexPathForRowAtPoint:CGPointMake(self.tableView.bounds.size.width/2, self.tableView.bounds.size.height/2)];
UITableViewCell *centerCell = [self.tableView cellForRowAtIndexPath:indexPathAtCenter];
Alternatively you could get all visible indexPaths and choose the middle one:
NSArray<NSIndexPath*> *visibleIndexPaths = [self.tableView indexPathsForVisibleRows];
NSIndexPath *middleIndexPath = visibleIndexPaths[visibleIndexPaths.count/2];
UITableViewCell *centerCell = [self.tableView cellForRowAtIndexPath: middleIndexPath];
Depends on the rest of your implementation which route you want to go!
In Swift 4
For exact index number at center we can simply use:-
let middleIndex = ((heightTableView.indexPathsForVisibleRows?.first?.row)! + (heightTableView.indexPathsForVisibleRows?.last?.row)!)/2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With