How can I tell if a UITableView contains a specific NSIndexPath?

前端 未结 8 1751
予麋鹿
予麋鹿 2021-02-07 06:27

Here is the code I\'m using:

if (appDelegate.currentMainIndexPath != nil /* && doesPathExistInTableView */)
{
    [tblView scrollToRowAtIndexPath:appDele         


        
8条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 07:13

    A Swift adaptation of Kamran Khan's answer:

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

提交回复
热议问题