Получить позицию ячейки в UITableview
Я пытаюсь получить позицию ячейки в tableview. из этого кода я получаю позицию ячейки
int numberOfSections = [UITableView numberOfSections];
int numberOfRows = 0;
NSIndexPath *tempIndexPath = nil;
UITableViewCell *tempCell = nil;
for(int i=0;i<numberOfSections;i++)
{
numberOfRows = [UITableView numberOfRowsInSection:i];
for(int j=0;j<numberOfRows;j++
{
tempIndexPath = [NSIndexPath indexPathForRow:j inSection:i];
tempCell = [UITableView cellForRowAtIndexPath:tempIndexPath];
NSLog("Y postion for cell at (Row = %d,Section = %d) is :%f",tempCell.frame.origin.y);
}
}
он вычисляет оси y ячейки в представлении таблицы, но моя проблема заключается в том, что я хочу получить позицию ячейки в видимой области rectview таблицы. т.е. если вид таблицы имеет размер cgrectmake (0,0,200,500)
и высота ячейки - 100 пикселей. поэтому tableview покажет 5 строк. если я прокручу вниз и выберите 7-ю строку. я получу его оси y 700. Но ячейка будет находиться в пределах (0,0,200,500) кадра.
Как я могу получить ячейку в этом кадре (0,0,200,500).
Ответы
Ответ 1
ПРИМЕЧАНИЕ. Надеюсь, вопрос может быть обновлен снова, пожалуйста, используйте другие ответы, которые могут быть применимы к вам.
Вы можете использовать метод visibleCells
, который будет возвращать видимые в данный момент ячейки в вашем представлении таблицы:
NSArray *visibleCellsList=[tableview visibleCells];
for (UITableViewCell* currentCell in visibleCellsList) {
NSLog(@"cell : %@ \n\n",currentCell.textLabel.text);
}
Ответ 2
Чтобы получить положение UITableViewCell
относительно вида, в котором он расположен (например, относительно видимой области):
Свифт 3, Свифт 4:
let rectOfCellInTableView = tableView.rectForRow(at: indexPath)
let rectOfCellInSuperview = tableView.convert(rectOfCellInTableView, to: tableView.superview)
print("Y of Cell is: \(rectOfCellInSuperview.origin.y)")
Objective-C:
CGRect rectOfCellInTableView = [tableView rectForRowAtIndexPath: indexPath];
CGRect rectOfCellInSuperview = [tableView convertRect: rectOfCellInTableView toView: tableView.superview];
NSLog(@"Y of Cell is: %f", rectOfCellInSuperview.origin.y);
Swift 2:
let rectOfCellInTableView = tableView.rectForRowAtIndexPath(indexPath)
let rectOfCellInSuperview = tableView.convertRect(rectOfCellInTableView, toView: tableView.superview)
print("Y of Cell is: \(rectOfCellInSuperview.origin.y)")
Ответ 3
Я сделал это вот так:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Это все, что я должен был сделать, чем все cell.frame
, cell.bounds
и т.д.