简体   繁体   中英

How to get the TableViewCell above (or below) a specific TableViewCell

Could anyone tell me how to access the UITableViewCell above (or below) a specific cell? I have access to the cell the user is selecting using didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell?
}

And I know how to access the cell's row number:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var currentRowNum = indexPath.row
}

Is there a way to access cells one row above or below?

All help is much appreciated!

Get like this.

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //Current cell...       
       var currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell?

       //Next cell...
       let nextIndex = NSIndexPath(forRow: indexPath.row + 1, inSection: indexPath.section)
       var nextCell = tableView.cellForRowAtIndexPath(nextIndex) as UITableViewCell?

      //Previous cell...
      let prevIndex = NSIndexPath(forRow: indexPath.row - 1, inSection: indexPath.section)
      var prevCell = tableView.cellForRowAtIndexPath(prevIndex) as UITableViewCell?
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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