#author("2016-05-30T15:12:37+09:00","default:wikiwriter","wikiwriter")
#author("2016-11-17T20:54:43+09:00","default:wikiwriter","wikiwriter")
[[UIKit/テーブル]]

&tag(UIKit/テーブル/セルのカスタマイズ);
*目次 [#gd626c5c]
#contents
*関連ページ [#sbb107cd]
*参考情報 [#xa37e514]

*ストーリーボードで設定したプロトタイプセルを利用する [#nbc22bb9]

-参考: [[[iOS] Auto Layout + Storyboard で高さ可変のUITableViewCellを作成する | Developers.IO:http://dev.classmethod.jp/smartphone/iphone/how-to-make-adjustable-cell/]]
-コードじゃなくてストーリーボードでカスタムセルを使用することができる。
-追加したプロトタイプセルのスタイルをBasicにすれば通常のセルのように使うこともできる。
**手順 [#ie11db94]
-ストーリーボードでUITableViewにUITableViewCellをドロップ(これがプロトタイプセルと呼ばれる)。
-UITableViewCellのセルを自作のクラスに変更。例MyCell。
-ストーリーボード上でセルにラベルなどを配置して、MyCellにアウトレットなどを設定。
#pre{{
class MyCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var descriptionLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
}}
-このセルを呼び出す。
#pre{{
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyCell
        cell.titleLabel?.text = rows[indexPath.row]
//        cell.textLabel?.text = rows[indexPath.row]
        return cell
    }
}}

*カスタマイズしたセルのマージンを調節する [#p46d6e8d]
**UILabelを設置する場合 [#nd9c03a2]
-UITableViewCellにUILabelを設置した場合、左側のマージンがデバイスによって変わってしまう問題。
-[[ios - Matching left alignment of custom & standard UITableViewCell types - Stack Overflow:http://stackoverflow.com/questions/29349917/matching-left-alignment-of-custom-standard-uitableviewcell-types]]
-ContentViewとUITableViewCellのPreserve Superview Marginsをtrueに設定し、UILabelのマージンをmarginに対して0とすればよい。

**UITextViewを設置する場合 [#he7bfe07]
-基本的にUILabelの場合と同じだが、内部の隙間が存在する。以下の設定で揃えられる
 textView.textContainer.lineFragmentPadding = 0



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS