情歌對唱的聊天室畫面
cell 的 auto layout, UITextView 高度 = 文字內容高度, UITableViewDataSource
成品:男女對唱歌很多,完全沒合唱還連句接唱的只想到這首 😆
▲有合唱的話聊天室就會看起來很怪嘛 =3=
▲私心覺得找香港男歌手唱整首粵語應該好聽百倍…
▲不過這下網誌就西洋+日本+台語+粵語+國語音樂全湊齊啦
▲手機聊天室裡圖文沒這麼大,但為了看清楚將就醜一下囉
▲參考來源:彼得和學長
前置作業
▲放底圖後 + TableView + TableViewCell * 2 ,2 者背景選無色
▲新增 Swift File * 1 + Cocoa Touch Class * 2,後 2 者取名後各自配好
▲ TableView 不要分線 Separator 調 None
▲ TableViewCell ID 取名,RowHeight 本有顯示,刪除後自動調整高度
▲加 ImageView,autoLayout 調 1 : 1 正方形
▲加 TextView,Scrolling Enabled 取消勾選
➞ Content Compression Resistance Priority Horizontal 750 改 1000 使換行
▲ StackView : ImageView + TextView,水平軸,Alignment : Center
➞ autoLayout 上下左右間距 10,1 頭 1 尾改 ≥ 10 ,長度自動調整
▲ TableView 拉至 ViewController 選 dataSource ↓
✅ 指定 data source / delegate 是型別 A 生成的東⻄
Girl/BoyTableViewCell.swift:拉 Outlet 設圓角,調整文字上下左右間距
用
struct
定義 Lyric 型別並寫其 Array 於 ViewController (未全列完)
TableView 拉 Outlet 至 ViewController
@IBOutlet weak var tableView: UITableView!
✅ 定義型別 A 遵從 protocol:UITableViewDataSource,寫 extension
extension ViewController: UITableViewDataSource {
}
✅ 在型別 A 裡定義 protocol 的 function
▲ cell 數量等於 lyrics Array 數量
▲依頭像照片辨別是男是女來套用不同 cell 內容
▲ withIdentifier 用 2 種不同方法寫,第 3 種請參考上篇
完整程式碼:Lyric.swift,ViewController
GirlTableViewCell,BoyTableViewCell