UITableViewCellがタップされた時の背景色を変更する
UITableViewCellのUITableViewCellSelectionStyleを変更すればよい。
標準では以下の4種類のスタイルが用意されてある。
- UITableViewCellSelectionStyle.Default
- UITableViewCellSelectionStyle.Blue
- UITableViewCellSelectionStyle.Gray
- UITableViewCellSelectionStyle.None
例えば、背景色を表示させないようにする場合は以下の実装になる。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("UniqCell") as! UITableViewCell cell.selectionStyle = UITableViewCellSelectionStyle.None }
上の4種類に含まれていない背景色にするには、selectedBackgroundViewに任意の色のUIViewを代入すれば実現できる。
赤色にする場合は以下のようになる。
... // selectionStyleを使用しないので、UITableViewCellSelectionStyleNoneを設定。 cell.selectionStyle = UITableViewCellSelectionStyleNone let selectedView = UIView() selectedView.backgroundColor = UIColor.redColor() cell.selectedBackgroundView = selectedView