Objective c UITableViewCell自定义属性(以Swift格式)
我希望能够以编程方式将自定义属性赋予UITableViewCell(或任何其他对象) 我在Swift w/Realm.iodb中使用委托方法(但我猜它应该与objective-c中的类似)。使用下面的注释行==是否正确Objective c UITableViewCell自定义属性(以Swift格式),objective-c,xcode,uitableview,swift,realm,Objective C,Xcode,Uitableview,Swift,Realm,我希望能够以编程方式将自定义属性赋予UITableViewCell(或任何其他对象) 我在Swift w/Realm.iodb中使用委托方法(但我猜它应该与objective-c中的类似)。使用下面的注释行==是否正确 override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { let cell = tabl
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell
let object = array[UInt(indexPath!.row)] as Language
cell.textLabel.text = object.title
cell.position = object.position // does not produce any warnings
return cell
}
属性title工作得很好(因为它是一个标签),但如何将其他(未声明的)属性属性属性赋予单元格,以便在以后遍历tableView时恢复它们
for var row = 0; row < tableView.numberOfRowsInSection(0); row++ {
var cellPath = NSIndexPath(forRow: row, inSection: 0)
var cell:Cell = tableView.cellForRowAtIndexPath(cellPath) as Cell
println(cell) // This prints out my cells, which contains a .text property, but no custom properties
}
print
使用的description
方法打印出对象。因为您没有提供自定义描述,所以只看到常规UITableViewCell的描述。您可以简单地打印(cell.position)
或:
var description: String { return "Cell with position \(position)" }
为什么您试图在单元格中存储数据,而不是在数据/其他数据源的
数组中存储数据?如果您真的愿意,您当然可以使用自定义的UITableViewCell
子类并声明您自己的属性。因为我正在尝试实现表单元格的重新排序,除了暂时存储每个单元格的初始位置值外,没有什么比这更好的了。。无论如何,我在我的控制器上使用一个自定义UITableViewCell(请参阅我更新的帖子)。我如何在其中实现这个自定义属性并在以后使用它?如果你有一个自定义的单元格
类,你可以在其中添加一个。@jtbandes我已经在我的自定义单元格类中添加了var position:Int?
,但是直接属性值没有多大作用…:Cell.position=object.position
“没有多大作用”意味着什么。。。请找出一个问题的简单演示,并更新您的帖子。
class Cell: UITableViewCell {
var position:Int?
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
}
}
var description: String { return "Cell with position \(position)" }