Ios 使用dequeueReusableCellWithIdentifier指定单元格时停止受影响的随机UITableViewCells
我对iOS和快速开发非常陌生,但到目前为止我很喜欢它 我有一个可滚动的tableview,它包含许多由自定义结构数组填充的单元格Ios 使用dequeueReusableCellWithIdentifier指定单元格时停止受影响的随机UITableViewCells,ios,uitableview,swift,Ios,Uitableview,Swift,我对iOS和快速开发非常陌生,但到目前为止我很喜欢它 我有一个可滚动的tableview,它包含许多由自定义结构数组填充的单元格 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("platformCell", f
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("platformCell", forIndexPath: indexPath) as! PlatformCell
cell.platformName.text = KeyPlatformsViewController.platforms[indexPath.row].platformName
cell.backgroundColor = UIColor(rgba: KeyPlatformsViewController.platforms[indexPath.row].platformColour)
return cell
}
问题是我想在代码中加入如下内容:
if cell.platformName.text == "A" {
cell.backgroundColor = UIColor.whiteColor();
}
但是对于deque单元格(我认为),在滚动视图之后,错误的单元格被定位
有人知道如何确保只有我选择的一个受到影响吗
我希望我说得很清楚,我认为我没有很好地解释这一点。因为,要根据某些数据条件更改文本的颜色,应该检查或验证数据(在您的情况下是数据源中的文本内容),而不是UI(单元格中的文本集) 因此,与其检查
如果cell.platformName.text==“A”
检查
如果KeyPlatformsViewController.platforms[indexath.row].platformName==“A”
通过这种方式,换句话说,您的数据(模型)驱动您的UI(视图)。当您基于if语句设置单元格中某个内容的值时,您应该始终包含一个“else”子句来处理其他情况(或其他情况,在这种情况下,您需要一些if else)
在哪里添加if cell.platformName.text==“A”etc代码?在自定义单元格类的.m中?@LeeJPollard我将在我发布的第一个方法中的返回单元格调用之前添加它。请尝试在自定义单元格类的.m中添加该代码。基本上,尝试使用if self.platformName.text==“A”代码重载单元格platformName属性的setter。理想情况下,您在单元格上所做的任何自定义UI工作都应该在自定义单元格类本身中完成。@LeeJPollard将其粘贴在override func setSelected()方法中不起作用-因为在中,它也会对其他单元格起作用。如果您在自定义单元格类中包含If self.platformName.text==“A”代码,然后它只会影响platformName.text==“A”的单元格。请使用更新的代码编辑您的问题,以便更好地理解。
if KeyPlatformsViewController.platforms[indexPath.row].platformName == "A" {
cell.backgroundColor = UIColor.whiteColor();
}else{
cell.backgroundColor = ... // default color
}