簡體   English   中英

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

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

這是我正在使用的代碼:

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

你可以用這個。 傳遞它 indexpath 的行和部分

目標 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
}

Kamran Khan 回答的 Swift 改編版:

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)
    }
}

有一種更方便的方法來判斷 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)
}

如果您的意思是,“索引 n 處是否有單元格”,那么您只需將數據源的大小與 n 進行比較

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

例如,數據源是一個 NSArray。

這是你如何做到的:

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

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

我重復了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;
}

您可以嘗試通過以下方式獲取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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM