[英]How do I transfer multiple selected table view cells data to the next view controller in Swift?
因此,我已经为此准备了很多代码,但出现了一些错误:
我当前的代码是:
func createGroupMessagesButton() {
dismissViewControllerAnimated(true) {
let user = self.tableView.indexPathsForSelectedRows
self.messagesController2?.showChatLogController(user)
}
}
上面的代码旨在关闭当前视图控制器,并将所有数据传递到下一个视图的函数中。 该功能代码为:
func showChatLogController(user: User) {
let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
chatLogController.user = user
chatLogController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(chatLogController, animated: true)
}
然后,上面的函数将数据传递到上面的函数,然后推送到另一个控制器。
唯一的问题是,当我第一次尝试传递数据时,出现一个错误,指出:
无法转换[NSIndexPath]类型的值? 预期类型参数User
PS:用户是我创建的数组。
这是我的用户数组:
类用户:NSObject {
var id: String!
var fullName: String!
var email: String!
var userPhoto: String!
var homeAddress: NSArray!
var schoolOrWorkAddress: String!
}
总结我的问题,我在传递多个选定的表视图单元格的数据时遇到了麻烦。
如果您想知道如何传递一个选定的单元格数据,请执行以下操作:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView.allowsMultipleSelectionDuringEditing != true {
dismissViewControllerAnimated(true) {
let user = self.users[indexPath.row]
self.messagesController?.showChatLogController(user)
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! UserCell
let user = users[indexPath.row]
cell.textLabel?.text = user.fullName
cell.detailTextLabel?.text = user.email
if let userPhoto = user.userPhoto {
cell.profileImageView.loadImageUsingCacheWithUrlString(userPhoto)
}
return cell
}
NSIndexPath
是用于获取到特定部分中特定行的路径的构造。
self.tableView.indexPathsForSelectedRows
返回选择的行的列表,因此您将不得不遍历它们,并使用row
属性在完整的用户列表中查找对应的用户。 还要注意,您很可能希望传递一个User
Array
,而不只是一个。
这是我头上的概念代码,应该可以引导您正确的方向。
func createGroupMessagesButton() {
dismissViewControllerAnimated(true) {
let selectedUserRows = self.tableView.indexPathsForSelectedRows
var selectedUsers = [User]
for let selectedUserRow in selectedUserRows {
selectedUsers.append(self.users[selectedUserRow.row]!)
}
self.messagesController2?.showChatLogController(selectedUsers)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.