Подтвердить что ты не робот

Как я могу определить, содержит ли UITableView определенный NSIndexPath?

Вот код, который я использую:

if (appDelegate.currentMainIndexPath != nil /* && doesPathExistInTableView */)
{
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    appDelegate.currentMainIndexPath = nil;
}
4b9b3361

Ответ 1

Вы можете попытаться получить UITableViewCell по:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// returns nil if cell is not visible or index path is out of range

здесь полный код:

UITableViewCell *cell = [cellForRowAtIndexPath:appDelegate.currentMainIndexPath];

if (appDelegate.currentMainIndexPath != nil && cell !=nil)
{
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    appDelegate.currentMainIndexPath = nil;
}

Ответ 2

Вы можете использовать это. Передайте строку и раздел индексного пути

-(BOOL) isRowPresentInTableView:(int)row withSection:(int)section
{
    if(section < [self.tableView numberOfSections])
    {
        if(row < [self.tableView numberOfRowsInSection:section])
        {
            return YES;
        }
    }
    return NO;
}

Ответ 3

Быстрая адаптация ответа Камран-хана:

extension UITableView {
  func hasRowAtIndexPath(indexPath: NSIndexPath) -> Bool {
    return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRowsInSection(indexPath.section)
  }
}

Swift 4:

extension UITableView {
    func hasRow(at indexPath: IndexPath) -> Bool {
        return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
    }
}

Ответ 4

Существует более удобный способ узнать, действительно ли indexPath:

Для Swift 3.0:

open func rectForRow(at indexPath: IndexPath) -> CGRect

Для Objective-C

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

Вы получите CGRectZero, если indexPath недействителен.

func isIndexPathValid(indexPath: IndexPath) -> Bool {
    return !tableView.rectForRow(at: indexPath).equalTo(CGRect.zero)
}

Ответ 5

Если вы имеете в виду, "есть ли ячейка с индексом n", вам просто нужно сравнить размер вашего источника данных с n

if (appDelegate.currentMainIndexPath != nil [datasource count] > n)
{
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    appDelegate.currentMainIndexPath = nil;
}

Где источником данных является, например, NSArray.

Ответ 6

Вот как вы это делаете:

indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]

В контексте:

if (indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]) {
    [self.tableView scrollToRowAtIndexPath:indexPath
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:YES];
}

Вставляется в ваш код:

if (appDelegate.currentMainIndexPath != nil &&
    indexPath.row < [tblView numberOfRowsInSection:indexPath.section]) {
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath
                   atScrollPosition:UITableViewScrollPositionTop
                           animated:NO];
    appDelegate.currentMainIndexPath = nil;
}

Ответ 7

Вы также можете использовать метод UITableView numberOfRowsInSection.