0
テーブルビューでセルを生成しようとしていますが、右側には動的に許可されたボタンがあります。いくつかは2つのボタンで、いくつかは4つのボタンで。問題は、私が望むようにテーブルビューがロードされますが、スクロールすると、すべてのセルが4つのボタンを持つことになります。スウィフトでテーブルセルを継承する方法
私は間違っていますか?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row][0]
cell.accessibilityLabel = self.items[indexPath.row][bigSort]
cell.selectionStyle = .None
for x in 0..<4
{
let button = UIButton()
let marginStart = w - tickWidthInCell * 4 - 9 + (tickWidthInCell + 2) * x
let margin = CGFloat(marginStart)
button.frame = CGRectMake(margin, CGFloat(cellHeightMargin), CGFloat(tickWidthInCell), CGFloat(tickHeightInCell))
button.layer.borderColor = GRAY.CGColor
if (self.items[indexPath.row][1] == "Special" && x % 2 != 0)
{
button.backgroundColor = UIColor.clearColor()
button.layer.borderColor = UIColor.clearColor().CGColor
//button.hidden = true
}
else
{
button.addTarget(self, action: #selector(cellButtonClicked), forControlEvents: .TouchUpInside)
button.backgroundColor = UIColor(netHex:0x222222)
button.layer.borderWidth = 1
}
cell.contentView.addSubview(button)
}
return cell
}
コードの重要な部分が欠落していると思います。セルをデキューする場所の全体的な方法を教えてください。 –
セルのスクロール方法で何かを使うと、セルが再利用され、それが起こります。 –
スクロールすると、セルが再利用されることを覚えておいてください。セルクラスの 'prepareForReuse'をオーバーライドし、そこの初期の初期化からどの状態もクリーンアップすることによって、あなたのセルが再利用されることを保証する必要があります。 – Shripada