Search code examples
swiftuitableviewuiscrollviewcellnsindexpath

TableView Determine Third Cell From Top


I constantly am trying to determine the third cell from the top of my table view. Basically, what that means is, the third cell from the top will always look different from all the others (i.e. the text color will change etc.). I figured since the cells are reused, I would always be able to access the third cell like this:

    

if (indexPath.row == 2) {
    
    }

Unfortunately, it doesn't seem to be working like that. When I go ahead and print the indexPath.row the numbers continue to increase all the way from 0 to 12... Now this is understandable since it is that cells row, but how may I go about accessing the third row always from the top. Here is the original approach I took:

    

override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let indexPath: NSIndexPath = self.tableView.indexPathsForVisibleRows![0]
            
        if (indexPath.row == 2) {
          // Print or whatever
    
            }
        } 
    }

So how can I go about accessing the third row always from the top of the tableView?

Thank you!


Solution

  • Here is an example project. I tried running it in the simulator and it seems to work fine.

    Here is a screenshot of XCode so you can see Main.Storyboard.

    Screenshot

    Video Walkthrough created with licecap

    Also here is a copy of the code in ViewController.swift:

    import UIKit
    
    class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    
    var indexOfThirdVisible: Int!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationController?.navigationBar.barStyle = .BlackTranslucent
        self.navigationController?.navigationBar.tintColor = UIColor.orangeColor()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let indexPath = self.tableView.indexPathsForVisibleRows![0]
        indexOfThirdVisible = indexPath.row + 2
        let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible, inSection: indexPath.section)) as! TableViewCell
        cell.label.textColor = UIColor.orangeColor()
    
        let cellAbove = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible - 1, inSection: indexPath.section)) as! TableViewCell
        let cellBelow = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexOfThirdVisible + 1, inSection: indexPath.section)) as! TableViewCell
        cellAbove.label.textColor = UIColor.whiteColor()
        cellBelow.label.textColor = UIColor.whiteColor()
    
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    // heightForRowAtIndexPath
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100
    }
    // configure cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
        return cell
    }
    }
    

    Here is TableViewCell.swift:

    import UIKit
    
    class TableViewCell: UITableViewCell {
    
    @IBOutlet weak var label: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
        }
    
    }
    

    Sorry if the indentation is messed up. Have a good day and let me know if you need any more help!