在我的 Xcode 项目中,我希望有一个类似 Snapchat 的“发送到...”屏幕的 View (我已附上屏幕截图)。我已经制作了一个表格 View 并填充它并允许进行多项选择。我目前在两件事上遇到了麻烦:
1) 多选:我可以选择我想要的单元格,但是当我点击搜索栏并开始输入时,我之前的所有选择都会消失。我假设我需要将所有名称添加到一个数组中,并以某种方式将数组与表进行通信,以便它显示该用户名是否在数组中,然后在 TableView 中将其选中。但我不确定该怎么做。 我该怎么做?
2) 发送到底部栏(照片中的蓝色):如您所知,在 Snapchat 中,当您按下要将快照发送给的用户时,他们的名字会添加到栏中在底部,当您填满栏时,它可以滑动,您可以在其中水平滚动浏览已添加的名称。我可以将名称附加到一个数组,并像他们一样在标签中显示该数组,但我不知道如何制作它以便用户可以水平滚动浏览它。我如何实现相同的功能?
请随时回答任何问题!你不需要做所有这些,我只需要他们回答。到目前为止,这是我的代码:
class User {
var userID:String?
var userFullName:String?
var userUsername:String?
var userProfileImage:PFFile?
var isPrivate:Bool
init(userID : String, userFullName : String, userUserName : String, userProfileImage : PFFile, isPrivate : Bool) {
self.userID = userID
self.userFullName = userFullName
self.userUsername = userUserName
self.userProfileImage = userProfileImage
self.isPrivate = isPrivate
}
}
var userArray = [User]()
func loadFriends() {
//STEP 1: Find friends
let friendsQuery = PFQuery(className: "Friends") //choosing class
friendsQuery.whereKey("friendOne", equalTo: PFUser.current()?.objectId ?? String()) //finding friends
friendsQuery.limit = self.page //number of users intitally showing
friendsQuery.findObjectsInBackground (block: { (objects, error) -> Void in
if error == nil { //if no error
//clean up
self.friendsArray.removeAll(keepingCapacity: false)
//STEP 2: Find related objects depending on query setting
for object in objects! {
self.friendsArray.append(object.value(forKey: "friendTwo") as! String) //hold array info of friend
}
//STEP 3: Find friend info
let query = PFUser.query()
query?.whereKey("objectId", containedIn: self.friendsArray)
query?.addDescendingOrder("createdAt") //how to order users
query?.findObjectsInBackground(block: { (objects, error) -> Void in
if error == nil {
for object in objects! {
var user : User
let fullname = (object.value(forKey: "fullname") as! String)
let username = (object.object(forKey: "username") as! String)
let profilePhoto = (object.object(forKey: "profilePhoto") as! PFFile)
let objectID = (object.objectId!)
let isPrivate = (object.object(forKey: "isPrivate") as! Bool)
user = User(userID: objectID, userFullName: fullname, userUserName: username, userProfileImage: profilePhoto, isPrivate: isPrivate)
self.userArray.append(user)
}
self.tableView.reloadData()
} else {
print(error!)
}
})
} else {
print(error!)
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! FriendCell
let user = userArray[indexPath.row]
//add user info to cells
cell.fullnameLabel.text = user.userFullName
cell.usernameLabel.text = user.userUsername
cell.objectID = user.userID!
cell.isPrivate = user.isPrivate
user.userProfileImage?.getDataInBackground (block: { (data, error) in
if error == nil {
cell.profilePhoto.image = UIImage(data: data!)
}
})
})
}
最佳答案
1) 多项选择:
您应该有一个 User 类(例如 User)来保存用户属性,而不是为每个属性维护 array
。将 User 对象存储在 Array
中。用户类可能如下所示:
class User {
var userID:String
var userFullName:String
var userName:String
var userProfileImageUrl:String
init(userID:String,userFullName:String,userName:String,userProfileImageUrl:String) {
self.userID = userID
self.userFullName = userFullName
self.userName = userName
self.userProfileImageUrl = userProfileImageUrl
}
}
您可以使用用户扩展来检查该用户是否被选中(例如,isSelected)。
import UIKit
import Foundation
private var selectedKey: UInt8 = 0
extension User {
var isSelected:Bool{
get {
return objc_getAssociatedObject(self, &selectedKey) as! Bool
}
set {
objc_setAssociatedObject(self, &selectedKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
现在在您的 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
中检查 user.isSelected == true/false
并更新您选择的/相应地取消选择图像。
并在 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
isSelected
的值
2) 发送到底栏:
对于底部栏,添加一个 UICollectionView
作为 UIView
中的 subview 。创建一个重写包含 UILabel
的 UICollectionViewCell
的类。您可以在 UICollectionView
中添加 流式布局
。
我只是给出了一个开始的想法。希望它能帮助你。
关于ios - Tableview Like Snapchat 发送功能(多选和可滑动标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45788764/