Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在uitableview didselect方法中在单个变量中存储多个值_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在uitableview didselect方法中在单个变量中存储多个值

Ios 如何在uitableview didselect方法中在单个变量中存储多个值,ios,swift,uitableview,Ios,Swift,Uitableview,我是swift新手,我不知道如何在单个值中传递多个值。我可以在进入下一页时使用多个选择。那么如何在singlevalue中存储多个值。有人帮我吗??我的代码是 var strRoomType = "" var strroomtype = [] func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let proModel = arrSpaceData[indexPath.row]

我是swift新手,我不知道如何在单个值中传递多个值。我可以在进入下一页时使用多个选择。那么如何在singlevalue中存储多个值。有人帮我吗??我的代码是

var strRoomType = ""  
var strroomtype = []
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let proModel = arrSpaceData[indexPath.row] as? RoomPropertyModel
    let propertyView = self.storyboard?.instantiateViewController(withIdentifier: "DepartureLocationVC") as! DepartureLocationVC
    propertyView.strRoomType = (proModel?.property_id as String?)!
  //  propertyView.strroomtype = (proModel?.property_id as String?)!
    self.navigationController?.pushViewController(propertyView, animated: true)
}

您必须收集所有选定行的
indexpath
。获取所选
indexpaths
的数组
对象/模型
,并将它们存储在单个数组中。然后,您可以将整个阵列传递到下一个
viewcontroller
或您想要使用的任何位置。如果要在行选择中导航到下一个
viewcontroller
,则始终会有一个
object/model
,因此不需要数组。只需将整个对象/模型传递给下一个
viewcontroller

您必须收集所有选定行的
indexpath
。获取所选
indexpaths
的数组
对象/模型
,并将它们存储在单个数组中。然后,您可以将整个阵列传递到下一个
viewcontroller
或您想要使用的任何位置。如果要在行选择中导航到下一个
viewcontroller
,则始终会有一个
object/model
,因此不需要数组。只需将整个对象/模型传递给下一个
viewcontroller

假设您有两个控制器,其中一个在TableView中显示学生列表,然后另一个控制器被推送,只显示单个学生信息

首先,您需要将所有需要的信息分组到
struct
class
中,我将使用struct,因为它是值类型。(如果您不知道,请查找参考与值类型)

假设我们进入了第一个包含
UITableView

这将是您设置tableView的方法:

1) 填充tableView的数组看起来像
arr=[Student]()

2) 假设您使用的是默认的
UITableViewCell
,您可以设置如下字段
cell.textlab.text=arr[indexPath.row].name
,以及
cell.detailtextlab?.text=String(arr[indexPath.age])

3)
numberofitemsinsection
将是
arr.count

我的意思是你明白要点了…

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        /* here you have many options 
           on how to pass the **selected data** to the detail controller.
         1) injecting the data into the detail controller via the initializer/ constructor  
         2) via property 
         3) injecting via method such as func setStudent(_ student: Student)
         4) etc...
        */

       let vc = DetailViewController(student: arr[indexPath.row])          
       self.navigationController?.pushViewController(vc, animated: true)
    }
详细视图控制器

final类DetailViewController:UIViewController{
私人出租学生:学生
私人出租名称标签:UILabel。。。。

init(student:student){/假设您有两个控制器,其中一个在TableView中显示学生列表,然后另一个控制器被推送,并且只显示单个学生信息

首先,您需要将所有需要的信息分组到
struct
class
中,我将使用struct,因为它是值类型。(如果您不知道,请查找引用vs值类型)

假设我们进入了第一个包含
UITableView

这将是您设置tableView的方法:

1) 填充tableView的数组看起来像
arr=[Student]()

2) 假设您使用的是默认的
UITableViewCell
,您可以设置如下字段
cell.textlab.text=arr[indexPath.row].name
,以及
cell.detailtextlab?.text=String(arr[indexPath.age])

3)
numberofitemsinsection
将是
arr.count

我的意思是你明白要点了…

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        /* here you have many options 
           on how to pass the **selected data** to the detail controller.
         1) injecting the data into the detail controller via the initializer/ constructor  
         2) via property 
         3) injecting via method such as func setStudent(_ student: Student)
         4) etc...
        */

       let vc = DetailViewController(student: arr[indexPath.row])          
       self.navigationController?.pushViewController(vc, animated: true)
    }
详细视图控制器

final类DetailViewController:UIViewController{
私人出租学生:学生
私人出租名称标签:UILabel。。。。

init(student:student){//为什么不把整个模型“RoomPropertyModel”传递给DepartureLocationVC??显然是使用数组/字典。它是已知的,但要获得名称、id、descriptionclass RoomPropertyModel:NSObject{var success\u message:NSString=”“var status\u code:NSString=“”var property_name:NSString!var property_id:NSString!var property_描述:NSString!func initiatePropertyData(ResponseDictionary:NSDictionary)->任意{property_name=MakentSupport().checkParamTypes(参数:ResponseDiction,键:“name”);返回self}那么,可以选择表中的多个单元格,然后执行一些操作,转到下一个控制器,传递这些选择的详细信息吗?为什么不传递整个模型“RoomPropertyModel”要离开LocationVC??显然要使用数组/字典。它是已知的,但要获取名称、id、descriptionclass RoomPropertyModel:NSObject{var success\u message:NSString=”“var status\u code:NSString=“”var property_name:NSString!var property_id:NSString!var property_描述:NSString!func initiatePropertyData(ResponseDictionary:NSDictionary)->任意{property_name=MakentSupport().checkParamTypes(参数:ResponseDiction,键:“name”);返回self}那么,我们可以选择表中的多个单元格,然后执行一些操作,转到下一个控制器,传递这些选择的详细信息吗?对不起,伙计,我没有现成的代码或演示。请尝试,否则我们将继续聊天。给你的链接::对不起,伙计,我没有现成的代码或演示。请尝试否则我们将继续聊天。您的链接::
final class DetailViewController: UIViewController {
   private let student:Student 
   private let nameLabel:UILabel ....

   init(student: Student) { //<--- injecting the student object into the controller 
     self.student = student 
     self.init(nibName: nil , bundle: nil)
   }

  // now you have the student do whatever you want with it 
  override func viewDidLoad() {
      super.viewDidLoad()
       nameLabel.text = student.name 
   }
}