我的Xcode是7.1,模拟器是iPhone6。
通常我使用dequeueResuableCellWithIdentifier
,但这一次我遇到了问题。
我的问题是:
在选择新行之后,它没有清除我的orangeColor()
,但打印行deselecting 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);
}
}
我的问题是:
1、dequeueReusableCellWithIdentifer
方法和cellForRowAtIndexPath
方法有什么主要区别?
2、什么时候使用第一个方法,什么时候使用第二个方法,我怎样知道呢?
这一次我从一个教程试着去做到另一个。
更新:
我有物理学背景和一点计算机科学知识,请不要因为我幼稚的问题等而生气。
dequeueReusableCellWithIdentifer
应仅在cellForRowAtIndexPath
的实现中使用。 - rmaddydequeueReusableCellWithIndentifier()
有更深入的理解了。我的理解是dequeue
会重用内存,但是cellForRowAtIndexPath
不会重用内存。我是正确的吗?我仍然不清楚为什么第二个会起作用,而第一个不会。 - joe