From e583dfd4bb5253078cf3d9170f4789d62a3d8419 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 11 Jan 2016 13:54:06 +0800 Subject: [PATCH] configure MentionUserCell --- Yep/Services/YepService.swift | 4 +++ .../ConversationViewController.swift | 6 ++-- Yep/Views/Mention/MentionView.swift | 32 +++++++++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index e57ba139..c3aa60d1 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -3696,6 +3696,10 @@ struct UsernamePrefixMatchedUser { let username: String let nickname: String let avatarURLString: String + + var mentionUsername: String { + return "@" + username + } } func usersMatchWithUsernamePrefix(usernamePrefix: String, failureHandler: ((Reason, String?) -> Void)?, completion: ([UsernamePrefixMatchedUser]) -> Void) { diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 7257d9e1..e5006b61 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -878,11 +878,11 @@ class ConversationViewController: BaseViewController { // view.addSubview(conversationFPSLabel) #endif - usersMatchWithUsernamePrefix("t", failureHandler: nil) { users in + usersMatchWithUsernamePrefix("i", failureHandler: nil) { users in println("usersMatchWithUsernamePrefix: \(users)") - delay(2) { [weak self] in - self?.mentionView + delay(1) { [weak self] in + self?.mentionView.users = users } } } diff --git a/Yep/Views/Mention/MentionView.swift b/Yep/Views/Mention/MentionView.swift index 3ec86256..3493648b 100644 --- a/Yep/Views/Mention/MentionView.swift +++ b/Yep/Views/Mention/MentionView.swift @@ -7,6 +7,7 @@ // import UIKit +import Navi private class MentionUserCell: UITableViewCell { @@ -14,6 +15,8 @@ private class MentionUserCell: UITableViewCell { return NSStringFromClass(self) } + static var avatarStyle: AvatarStyle = .RoundedRectangle(size: CGSize(width: 30, height: 30), cornerRadius: 15, borderWidth: 0) + lazy var avatarImageView: UIImageView = { let imageView = UIImageView() imageView.contentMode = .ScaleAspectFill @@ -29,7 +32,7 @@ private class MentionUserCell: UITableViewCell { return label }() - lazy var usernameLabel: UILabel = { + lazy var mentionUsernameLabel: UILabel = { let label = UILabel() label.textColor = UIColor.yepTintColor() label.font = UIFont.systemFontOfSize(14) @@ -46,19 +49,19 @@ private class MentionUserCell: UITableViewCell { func makeUI() { contentView.addSubview(avatarImageView) contentView.addSubview(nicknameLabel) - contentView.addSubview(usernameLabel) + contentView.addSubview(mentionUsernameLabel) avatarImageView.translatesAutoresizingMaskIntoConstraints = false nicknameLabel.translatesAutoresizingMaskIntoConstraints = false - usernameLabel.translatesAutoresizingMaskIntoConstraints = false + mentionUsernameLabel.translatesAutoresizingMaskIntoConstraints = false let views = [ "avatarImageView": avatarImageView, "nicknameLabel": nicknameLabel, - "usernameLabel": usernameLabel, + "mentionUsernameLabel": mentionUsernameLabel, ] - let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[avatarImageView(30)]-15-[nicknameLabel]-[usernameLabel]-15-|", options: [.AlignAllCenterY], metrics: nil, views: views) + let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[avatarImageView(30)]-15-[nicknameLabel]-[mentionUsernameLabel]-15-|", options: [.AlignAllCenterY], metrics: nil, views: views) let avatarImageViewCenterY = NSLayoutConstraint(item: avatarImageView, attribute: .CenterY, relatedBy: .Equal, toItem: contentView, attribute: .CenterY, multiplier: 1.0, constant: 0) let avatarImageViewHeight = NSLayoutConstraint(item: avatarImageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 30) @@ -66,12 +69,27 @@ private class MentionUserCell: UITableViewCell { NSLayoutConstraint.activateConstraints(constraintsH) NSLayoutConstraint.activateConstraints([avatarImageViewCenterY, avatarImageViewHeight]) } + + func configureWithUsernamePrefixMatchedUser(user: UsernamePrefixMatchedUser) { + + let plainAvatar = PlainAvatar(avatarURLString: user.avatarURLString, avatarStyle: MentionUserCell.avatarStyle) + avatarImageView.navi_setAvatar(plainAvatar, withFadeTransitionDuration: avatarFadeTransitionDuration) + + nicknameLabel.text = user.nickname + mentionUsernameLabel.text = user.mentionUsername + } } class MentionView: UIView { static let height: CGFloat = 125 + var users: [UsernamePrefixMatchedUser] = [] { + didSet { + tableView.reloadData() + } + } + lazy var tableView: UITableView = { let tableView = UITableView() tableView.backgroundColor = UIColor.redColor() @@ -115,11 +133,13 @@ extension MentionView: UITableViewDataSource, UITableViewDelegate { } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 10 + return users.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(MentionUserCell.reuseIdentifier, forIndexPath: indexPath) as! MentionUserCell + let user = users[indexPath.row] + cell.configureWithUsernamePrefixMatchedUser(user) return cell } }