17

これが私が使用しているコードです:

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

8 に答える 8

37

これを使用できます。indexpathの行とセクションを渡します

Objective C:

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

スウィフト3:

func isRowPresentInTableView(indexPath: IndexPath) -> Bool{
    if indexPath.section < tableView.numberOfSections{
        if indexPath.row < tableView.numberOfRows(inSection: indexPath.section){
            return true
        }
    }

    return false
}
于 2014-10-22T09:34:27.143 に答える
26

カムラン・カーンの答えの迅速な適応:

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

スウィフト4:

extension UITableView {
    func hasRow(at indexPath: IndexPath) -> Bool {
        return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
    }
}
于 2016-04-27T08:55:45.860 に答える
3

indexPathが有効かどうかを判断するためのより便利な方法があります。

Swift 3.0の場合:

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

Objective-Cの場合

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

indexPathが無効な場合は、CGRectZeroを取得します。

func isIndexPathValid(indexPath: IndexPath) -> Bool {
    return !tableView.rectForRow(at: indexPath).equalTo(CGRect.zero)
}
于 2016-08-15T06:42:04.217 に答える
2

つまり、'インデックスnにセルがあります'の場合、データソースのサイズをnと比較するだけです。

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

データソースがたとえばNSArrayである場合。

于 2012-05-08T20:57:08.220 に答える
2

これがあなたのやり方です:

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;
}
于 2014-02-06T08:54:54.660 に答える
0

numberOfRowsInSectionUITableViewのメソッドを使用することもできます。

于 2014-01-22T09:47:24.183 に答える
0

私はカムランの答えを繰り返しました:

+ (BOOL)isIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView {
    if (!indexPath) {
        return NO;
    }
    if (indexPath.section < [tableView numberOfSections]) {
        if (indexPath.row < [tableView numberOfRowsInSection:indexPath.section]) {
            return YES;
        }
    }
    return NO;
}
于 2019-12-02T01:10:43.150 に答える
-6

あなたは通り抜けることを試みることができます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;
}
于 2012-05-09T03:35:26.367 に答える