Swift TableView从顶部确定第三个单元格

Swift TableView从顶部确定第三个单元格,swift,uitableview,uiscrollview,cell,nsindexpath,Swift,Uitableview,Uiscrollview,Cell,Nsindexpath,我一直在尝试从我的表视图的顶部确定第三个单元格。基本上,这意味着,从顶部开始的第三个单元格将始终与所有其他单元格看起来不同(即,文本颜色将发生变化等)。 我想既然这些单元被重复使用,我就可以像这样访问第三个单元: if (indexPath.row == 2) {          } override func scrollViewDidScroll(scrollView: UIScrollView) {              let indexPath: NSIndexPath =

我一直在尝试从我的表视图的顶部确定第三个单元格。基本上,这意味着,从顶部开始的第三个单元格将始终与所有其他单元格看起来不同(即,文本颜色将发生变化等)。 我想既然这些单元被重复使用,我就可以像这样访问第三个单元:

if (indexPath.row == 2) {
    
    }
override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let indexPath: NSIndexPath = self.tableView.indexPathsForVisibleRows![0]
            
        if (indexPath.row == 2) {
          // Print or whatever
    
            }
        } 
    }
不幸的是,它似乎不是那样工作的。当我继续打印
indexPath.row
时,数字继续从0增加到12。。。现在这是可以理解的,因为它是单元格
,但是我如何始终从顶部访问第三行呢。以下是我最初采用的方法:

if (indexPath.row == 2) {
    
    }
override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let indexPath: NSIndexPath = self.tableView.indexPathsForVisibleRows![0]
            
        if (indexPath.row == 2) {
          // Print or whatever
    
            }
        } 
    }
那么,如何始终从
表视图的顶部访问第三行呢


谢谢大家!

我只实现了逻辑,您可以根据自己的需要定制一点

    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    let indexPath : NSIndexPath = (tv.indexPathsForVisibleRows as! NSArray).objectAtIndex(0) as! NSIndexPath

    let thirdRow = indexPath.row + 2
    if thirdRow <= dataArr.count{// dataArr is your no of rows

        let thirdIndexPath = NSIndexPath(forRow: thirdRow, inSection: 0)

        let cell = tv.cellForRowAtIndexPath(thirdIndexPath)
        // ---- here you can perform any task with third cell----

    }
}
func-scrollViewDiEndDraging(scrollView:UIScrollView,willDecreate-Decreate:Bool){
让indexath:nsindepath=(tv.indexPathsForVisibleRows为!NSArray)。objectAtIndex(0)为!nsindepath
设thirdRow=indexPath.row+2

如果thirdRow我只实现了逻辑,那么您可以根据自己的需要对其进行定制

    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    let indexPath : NSIndexPath = (tv.indexPathsForVisibleRows as! NSArray).objectAtIndex(0) as! NSIndexPath

    let thirdRow = indexPath.row + 2
    if thirdRow <= dataArr.count{// dataArr is your no of rows

        let thirdIndexPath = NSIndexPath(forRow: thirdRow, inSection: 0)

        let cell = tv.cellForRowAtIndexPath(thirdIndexPath)
        // ---- here you can perform any task with third cell----

    }
}
func-scrollViewDiEndDraging(scrollView:UIScrollView,willDecreate-Decreate:Bool){
让indexath:nsindepath=(tv.indexPathsForVisibleRows为!NSArray)。objectAtIndex(0)为!nsindepath
设thirdRow=indexPath.row+2

如果thirdRow这里是一个示例项目,我试着在模拟器中运行它,它似乎工作得很好

这是XCode的屏幕截图,您可以看到Main.Storyboard

此外,以下是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
}
}
以下是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
    }

}
对不起,如果缩进搞错了。
祝您愉快,如果您需要更多帮助,请告诉我!

这里是一个示例项目。我尝试在模拟器中运行它,它似乎工作正常

这是XCode的屏幕截图,您可以看到Main.Storyboard

此外,以下是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
}
}
以下是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
    }

}
对不起,如果缩进搞错了。
祝你过得愉快,如果你需要更多帮助,请告诉我!

你的意思是每3、6、9个这样的单元格或从顶部算起的第3个单元格,如第5个位于顶部,然后第7个将是第三个,那么你想要哪一个逻辑?@Dharbirsingh第二个解释。因此,基本上假设第5个单元格位于顶部,第7个单元格将是正在编辑的单元格。如果用户再向下滚动一个单元格,那么第六单元在上面,现在第八单元会变。这有意义吗?谢谢!请看我的答案。你是说每3,6,9这样,或者从上面算起的第三个,比如第五个在上面,然后第七个在第三个,那么你想要哪一个逻辑呢?@Dharbirsingh第二个解释。所以基本上假设第五单元在上面,第七单元就是被编辑的那个d、 如果用户再向下滚动一个单元格,那么第6单元格位于顶部,现在第8单元格将更改。这有意义吗?谢谢!请查看我的答案。