『Swift』tableViewのセルが選択された時の背景色を変えたい

ウェブ制作

セル選択時の背景色の変更方法か幾つも有るようだが、cellForRowIndexPathから変更ができるものをメモしておきます。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
var cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell
// 背景色
cell.backgroundColor = UIColor.clearColor()
// 選択された時の背景色
var cellSelectedBgView = UIView()
cellSelectedBgView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = cellSelectedBgView
return cell
}

『dequeueReusableCellWithIdentifier』の引数”tableCell”はIdentifierで指定したものを入力。
この例では、基本背景色はなし(clearColor)で赤色(redColor)に変更される。

コメント

タイトルとURLをコピーしました