【问题标题】:TableView Determine Third Cell From TopTableView 从顶部确定第三个单元格
【发布时间】:2016-01-14 05:50:38
【问题描述】:

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

    

if (indexPath.row == 2) {
    
    }

不幸的是,它似乎不是那样工作的。当我继续打印 indexPath.row 时,数字继续从 0 一直增加到 12... 现在这是可以理解的,因为它是单元格 row,但是我如何才能始终从顶端。这是我采用的原始方法:

    

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

那么我如何才能始终从tableView 的顶部访问第三行?

谢谢!

【问题讨论】:

  • 你的意思是每 3,6,9 次这样,或者从上到下第 3 次,比如第 5 次在上面,然后第 7 次将是第 3 次,那么你想要哪一种逻辑?
  • @DharmbirSingh 第二种解释。所以基本上可以说单元格 5 位于顶部,单元格 7 将是正在编辑的单元格。如果用户随后向下滚动了一个单元格,因此单元格 6 在顶部,那么现在单元格 8 将发生变化。那有意义吗?谢谢!
  • 请看我的回答。

标签: swift uitableview uiscrollview cell nsindexpath


【解决方案1】:

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

这是 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
    }

}

抱歉,如果缩进搞砸了。 祝您有美好的一天,如果您需要更多帮助,请告诉我!

【讨论】:

    【解决方案2】:

    我只是实现了逻辑,你可以根据你的需要定制一点。

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

    如果有什么问题请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 2017-03-09
      • 2021-08-30
      • 2017-08-22
      • 2019-03-03
      相关资源
      最近更新 更多