- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 Xcode 是 7.1。模拟器是iPhone6。
通常我使用dequeueResuableCellWithIdentifier
,但是这次我遇到了问题。
我的问题是:
选择新行后,它不会清除我的 orangeColor()
。但是取消选择 indexPath
的打印行是正确的。
我的解决方案是:
我从 let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) 更改为! MyCell;
到让 cell = tableView.cellForRowAtIndexPath(indexPath) 作为! MyCell;
然后它解决了我的问题。背景颜色是 clearColor()
。
这是我的文件:
我的细胞.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. 何时使用第一个或第二个,我怎么知道?这次我使用从一个教程到另一个教程的试错法。
更新:
我的背景是物理学和一点点计算机科学。请不要对我幼稚的问题生气等等。
最佳答案
tableview 尝试在内存中始终不包含所有索引路径的单元格对象实例:
'dequeueReusableCellWithIdentifer' 回收或创建一个单元格,应该在 'cellForRow...' 中调用。这总是返回一个单元格实例,一个新的或以前不再可见的单元格实例。之后准备要显示的单元格。
'cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?//如果 单元格不可见或索引路径超出范围,则返回 nil' 这不会创建单元格,只允许您访问它们。我认为应该尽可能避免,以防止意外泄漏和其他对 tableView 的干扰。
关于您的“setSelected”问题:
关于ios - tableView.dequeueReusableCellWithIdentifier VS tableView.cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559932/
我有一个问题,当从 tableView:cellForRowAtIndexPath 调用时,cellForRowAtIndexPath 总是返回 nil: 问题是我实际上是在 tableView:ce
假设 self.tableView 是我创建的 UITableView 实例变量,任何人都可以解释这两个语句之间的区别吗?两者在语法上都是正确的,而且我认为两者都会做同样的事情:在 self.tabl
我对 iPhone 开发还很陌生。因此,如果我问一些非常简单的问题,请耐心等待。 在我的应用程序中,我有多个 View (即 .xib 文件)。单击主视图(CouponWebsiteViewContr
Application Specific Information: *** Terminating app due to uncaught exception 'NSRangeException',
我正在显示通过 Storyboard 初始化的动态表格 View 。 没什么特别的,只是用文本标签初始化 3 个单元格。 以下是实际发生的情况: 2014-08-15 12:39:19.909 Pop
我正在开发一个应用程序,我有一个 UITableView,它使用 tableView 方法填充了一个 NSMutableArray:cellForRowAtIndexPath:,它工作正常,但问题是我
我在两个 View 之间沟通时遇到了麻烦。我的目标是向我的 NSMutableArray 添加一个新对象.感谢 stackoverflow 上可爱的人,他们已经修复了。现在我有另一个问题。 即使我能够
我有一个奇怪的错误。我有一个带有静态单元格的分组 UITableView,我想在每个部分下添加一个白色阴影。我考虑的方法是为一个部分中的每个最低单元格添加阴影。 所以在 viewDidAppear 中
每当我滚动我的 tableview 时,它都非常滞后。我认为这与我加载细胞的方式有关。我尽可能使用 UINib (5.0+),同时仍然提供向后兼容性。然后我从 NSArray 中的 NSDiction
我不明白cellForRowAtIndexPath函数的indexPath参数始终为0。我的表当前有7行。我的表格显示的是表格第一行的7倍。是什么导致indexPath始终为零? @implement
我正在开发一个使用表格 View 的 ToDoList iOS 应用程序。单元格内容(UITableCellView)由 nsmutablearray 的实例变量填充。 当我添加一个新的 ToDoIt
所以我有快速的代码,可以创建一个配置文件列表,并允许个人对其进行过滤和排序以及单击它们。就像地址簿一样。它以主/详细信息格式设置,其中主信息是人员列表,详细信息是个人。我还有一个来自 master 的
完整代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
我有两个实体:日记帐和帐户。这种关系是一对多的:Journal 只有一个 Account。账户有很多日记账。我想根据每个帐户的日记显示总计,但我不知道如何操作。目前我为每个帐户获得的总额是相同的: 在
我的 cellForRowAtIndexPath 并非每次都会触发我将 dataSource 和 delegate 设置给文件所有者。我还设置了引用 socket 。我还尝试了 tbl.delegat
我的项目: 我的项目中有一个 UITableView。每个 UITableViewCell 都有一个 UITextView 作为 subview 。当用户点击“添加”按钮时,我需要再添加一个带有 UI
我的单元格中的数据在 cellforrowatindexpath 中混淆了。我更改了标签的文本颜色和文本,但即使 isComplete 值更改,单元格颜色和文本也会随机更改。 例如,当我滚动表格 Vi
免责声明:我使用的是 iOS 8,因此这可能是一个错误。 我正在尝试在特定事件发生后以编程方式编辑 UITableView 中特定 IndexPath 处单元格的背景颜色。我使用以下代码: UITab
填充 TableView 时出现奇怪的错误。这是我做过无数次的事情,但现在我对一些可能非常简单的事情感到困惑。也许我只是醒得太久了。 我有一个名为 riverGauge 的对象,它负责从 Web 服务
我的应用程序中有一个 tableView,当用户点击该单元格时,我需要它转到另一个 View Controller ,该 Controller 提供有关他们单击的单元格的更多详细信息。当我尝试使用 获
我是一名优秀的程序员,十分优秀!