Nic*_*son 0 cell uitableview uiscrollview nsindexpath swift
我一直在尝试确定表格视图顶部的第三个单元格。基本上,这意味着从顶部算起的第三个单元格看起来总是与所有其他单元格不同(即文本颜色会发生变化等)。我想既然单元格被重用,我总是能够像这样访问第三个单元格:
if (indexPath.row == 2) {
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它似乎不是那样工作的。当我继续打印indexPath.row数字时,数字会从 0 一直增加到 12 ......现在这是可以理解的,因为它是 cell row,但是我如何总是从顶部访问第三行。这是我采用的原始方法:
override func scrollViewDidScroll(scrollView: UIScrollView) {
let indexPath: NSIndexPath = self.tableView.indexPathsForVisibleRows![0]
if (indexPath.row == 2) {
// Print or whatever
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能总是从 的顶部访问第三行tableView呢?
谢谢!
这是一个示例项目。我尝试在模拟器中运行它,它似乎工作正常。
这是 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
}
}
Run Code Online (Sandbox Code Playgroud)
这是 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
}
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果缩进搞砸了。祝您有美好的一天,如果您需要更多帮助,请告诉我!
| 归档时间: |
|
| 查看次数: |
640 次 |
| 最近记录: |