我的Xcode是7.1。模拟器是iPhone6。
通常我使用dequeueResuableCellWithIdentifier,但这一次我遇到了问题。
我的问题是:
在选择新行后,它不会清除我的orangeColor()。但是取消选择indexPath的打印行是正确的。
我的解决方案是:
我变了
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
至
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;
然后它解决了我的问题。背景色是clearColor()。
这里是我的文件:
MyCell.swift:
import UIKit
class MyCell: UITableViewCell {
@IBOutlet weak var myLabel : UILabel!
@IBOutlet weak var myWebView : UIWebView!;
//@IBOutlet weak var myView : UIView!;
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
}
}ViewController.swift :
import UIKit
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableViewObject: UITableView!
var fruits = ["Apple", "Banana","Cherry", "Durian", "ElderBerry"];
var selectedIndexPath : NSIndexPath?;
override func viewDidLoad() {
super.viewDidLoad()
self.tableViewObject.delegate = self;
self.tableViewObject.dataSource = self;
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return fruits.count;
}
func loadCellContent(acell : UITableViewCell, indexPath : NSIndexPath, color: UIColor){
let cell = acell as! MyCell;
cell.myLabel.text = String(UnicodeScalar(65 + indexPath.row));
cell.myWebView.loadHTMLString(fruits[indexPath.row], baseURL: nil);
cell.myWebView.scrollView.bounces = false;
cell.myWebView.scrollView.scrollEnabled = false;
cell.myWebView.userInteractionEnabled = false;
cell.backgroundColor = color;
cell.myLabel.backgroundColor = color;
cell.myWebView.backgroundColor = color;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
self.loadCellContent(cell, indexPath: indexPath, color: UIColor.clearColor());
return cell;
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;
self.loadCellContent(cell, indexPath: indexPath, color : UIColor.orangeColor());
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
//let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell;
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell;
self.loadCellContent(cell, indexPath: indexPath,color: UIColor.clearColor());
print("Hellos", indexPath.row);
}
}我的问题是:
dequeueReusableCellWithIdentifer和cellForRowAtIndexPath的主要区别是什么?更新:
我的背景是物理和一点计算机科学。请不要因为我天真的问题而生气。
发布于 2015-11-06 07:38:11
tableview试图在内存中的任何时候都没有所有索引路径的单元对象实例:
关于你的“setSelected”问题:
发布于 2015-11-06 18:20:00
这两种方法用途不同。
dequeueReusableCellWithIdentifier:indexPath:将实例化一个新的单元格,但将重用以前在屏幕上滚动过的单元格(如果可用的话)。
您只从tableView:cellForRowAtIndexPath:调用此方法。cellForRowAtIndexPath:方法(不要与同名的tableView:cellForRowAtIndexPath:方法混淆)不会实例化一个新的单元格,而是只用于获取对当前可见的以前实例化单元格的引用。如果单元格不可见(例如,在屏幕外滚动),则返回nil。
在tableView:didSelectRowAtIndexPath:和tableView:didDeselectRowAtIndexPath:等方法中使用此方法,因为您只是试图获取对现有单元格的引用,而不是实例化新的单元格。--
注意,在代码示例中,您不仅从loadCellContent调用tableView:cellForRowAtIndexPath:,而且也从tableView:didSelectRowAtIndexPath:和tableView:didDeselectRowAtIndexPath:调用。我建议将单元格的初始配置(例如,只从tableView:cellForRowAtIndexPath:调用的loadCellContent )与背景颜色的调整分离开来(您将在didSelect...和didDeselect...中这样做)。后两种方法通常不会重新配置整个单元格,而是只更新背景颜色(或执行任何适当的装饰以显示所选内容)。
此外,您还需要确保tableView:cellForRowAtIndexPath:检查在配置背景色时是否选中了单元格。用户可能会选择一个单元格,将其滚动到屏幕上,然后在屏幕上回滚,在这种情况下,tableView:cellForRowAtIndexPath:必须检查单元格是否被选中以设置背景色。
例如:
// MARK: Cell configuration
func loadContentForCell(cell: UITableViewCell, indexPath: NSIndexPath) {
// do whatever you need here; I'm just going to set the `textLabel`
cell.textLabel?.text = "Row \(indexPath.row)"
}
func updateBackgroundColorForCell(cell: UITableViewCell?) {
cell?.contentView.backgroundColor = cell?.selected == true ? UIColor.redColor() : UIColor.whiteColor()
}
// MARK: UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
loadContentForCell(cell, indexPath: indexPath)
updateBackgroundColorForCell(cell)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
updateBackgroundColorForCell(cell)
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
updateBackgroundColorForCell(cell)
}https://stackoverflow.com/questions/33559932
复制相似问题