简体   繁体   English

如何判断 UITableView 是否包含特定的 NSIndexPath?

[英]How can I tell if a UITableView contains a specific NSIndexPath?

Here is the code I'm using:这是我正在使用的代码:

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

You can use this.你可以用这个。 Pass it indexpath's row and section传递它 indexpath 的行和部分

Objective C:目标 C:

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

Swift 3:斯威夫特 3:

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

    return false
}

A Swift adaptation of Kamran Khan's answer: Kamran Khan 回答的 Swift 改编版:

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

Swift 4:斯威夫特 4:

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

There is a more convenient method to tell if a indexPath is valid:有一种更方便的方法来判断 indexPath 是否有效:

For Swift 3.0:对于 Swift 3.0:

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

For Objective-C对于 Objective-C

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

You will get CGRectZero if the indexPath is invalid.如果 indexPath 无效,您将获得 CGRectZero。

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

If you mean, 'is there a cell at index n' then you just have to compare the size of you datasource to n如果您的意思是,“索引 n 处是否有单元格”,那么您只需将数据源的大小与 n 进行比较

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

Where datasource is for instance an NSArray.例如,数据源是一个 NSArray。

This is how you do it:这是你如何做到的:

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

In context:在上下文中:

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

Inserted into your code:插入到您的代码中:

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

你也可以使用 UITableView 的numberOfRowsInSection方法。

I iterated upon Kamran's answer :我重复了Kamran 的回答

+ (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;
}

You could try to get UITableViewCell by :您可以尝试通过以下方式获取UITableViewCell

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

here's the complete code :这是完整的代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM