プログラムでカスタム プロパティを UITableViewCell (またはその他のオブジェクト) に関連付けられるようにしたいと考えています。
Swift w/ Realm.io DB でデリゲート メソッドを使用しています (ただし、objective-c でも似たようなものになるはずです)。以下のコメントアウトされた行を使用しています==これを行う正しい方法ですか?
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
}
タイトルの帰属は (ラベルであるため) うまく機能しますが、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
}
Println は次の結果を生成しますが、position プロパティは含まれていません。
<_TtC8Verbum_24Cell: 0x7f9fb58bd3d0; baseClass = UITableViewCell; frame = (0 0; 320 44); text = 'Italiano'; autoresize = W; layer = <CALayer: 0x7f9fb585bb70>>
<_TtC8Verbum_24Cell: 0x7f9fb58bb670; baseClass = UITableViewCell; frame = (0 44; 320 44); text = 'Francais'; autoresize = W; layer = <CALayer: 0x7f9fb58a44f0>>
更新:テーブルセルにカスタムクラスを使用しています:
class Cell: UITableViewCell {
var position:Int?
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
}
}