【发布时间】:2015-11-06 05:12:33
【问题描述】:
我的 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);
}
}
我的问题是:
1.dequeueReusableCellWithIdentifer和cellForRowAtIndexPath的主要区别是什么?
2. 何时使用第一个或第二个,我怎么知道?
这次我使用从一个教程到另一个教程的试验/错误。
更新:
我的背景是物理学和一点点计算机科学。请不要因为我的幼稚问题而生气。
【问题讨论】:
-
dequeueReusableCellWithIdentifer只能用于cellForRowAtIndexPath的实现中。 -
@Anbu.Karthik 感谢您的搜索。我现在对
dequeueReusableCellWithIndentifier()有了更多的了解。我的理解是dequeue会重用内存,但cellForRowAtIndexPath不会重用内存。我对吗?我仍然不清楚为什么第二个有效,但第一个无效。 -
@rmaddy 对不起,我不明白。你能举个例子给我解释一下吗?
-
@rmaddy。我现在明白了!谢谢你:)
标签: ios swift uitableview