先在StoryBoard把layout拉好
在inspector可以調內容物高寬的權重
建立一個UITableCellClass
把outlet拉進去
var tweet: Twitter.Tweet?{
didSet{
UpdateUI()
}
}
func UpdateUI() {
userTextLabel?.attributedText = nil
userScreenNameLabel?.text = nil
userImage?.image = nil
userCreateTimeLabel?.text = nil
userTextLabel?.text = tweet?.text
if userTextLabel?.text != nil {
for _ in (tweet?.media)! {
userTextLabel?.text! += " ?"
}
}
if let name = tweet?.user.screenName {
userScreenNameLabel?.text = name
}
if let profileImageURL = tweet?.user.profileImageURL{
DispatchQueue.global().async {
[weak weakMe = self] in
if let igData = NSData(contentsOf: profileImageURL as URL){
DispatchQueue.main.async {
self.userImage?.image = UIImage(data: igData as Data)
}
}
}
}
let formatter = DateFormatter()
if Date().timeIntervalSince(tweet?.created as! Date) > 24*60*60 {
formatter.dateStyle = DateFormatter.Style.short
}else {
formatter.timeStyle = DateFormatter.Style.short
}
userCreateTimeLabel?.text = formatter.string(from: tweet?.created as! Date)
}
Controller把tweet射進去
if let tweetCell = cell as? TweetMainCell {
tweetCell.tweet = tweet
}
在viewDidLoad設定Cell的高度
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
到info.plist加入
圖片才能顯示
增加TextFiled
var serchText : String? {
didSet{
tweets.removeAll()
searchForTweets()
title = serchText
}
}
@IBOutlet var searchTextField: UITextField!
{
didSet{
searchTextField.delegate = self
searchTextField.text = serchText
}
}
按下Return把鍵盤關閉
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
serchText = textField.text!
return true
}