2016-11-04 70 views
0

我有一个自定义的TableView,通过Json获取数据,我有一个名为“FullName”的tableView中的按钮。该FullName显然具有用户名,但OnClick我想获得与该特定TableViewCell相对应的“Profile_ID”,以便我可以保存它。我的代码将有助于明确的东西IOS Swift如何获取元素的值点击TableView

class HomePageViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 


    @IBOutlet var StreamsTableView: UITableView! 


    var names = [String]() 
    var profile_ids = [String]() 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     StreamsTableView.dataSource = self 

     let urlString = "http://"+Connection_String+":8000/streams" 

     let url = URL(string: urlString) 
     URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in 
      if error != nil { 
       /// print(error) 
      } else { 
       do { 

        let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any] 
        if let Streams = parsedData["Streams"] as! [AnyObject]? { 
       // Getting Json Values  
         for Stream in Streams { 
          if let fullname = Stream["fullname"] as? String { 
           self.names.append(fullname) 
          } 


          if let profile_id = Stream["profile_id"] as? String { 
           self.profile_ids.append(profile_id) 
          } 


          DispatchQueue.main.async { 
           self.StreamsTableView.reloadData() 
          } 

         } 


        } 



       } catch let error as NSError { 
        print(error) 
       } 
       print(self.names) 
      } 

     }).resume() 






    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func Fullname_Click(){ 
    // Where the # 32 is I would like to replace that with Profile_ID 
     UserDefaults.standard.set("32", forKey: "HomePage_Fullname_ID") 
     let navigate = self.storyboard?.instantiateViewController(withIdentifier: "Profiles") as? MyProfileViewController 
     self.navigationController?.pushViewController(navigate!, animated: true) 
    } 



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 

      tableView.backgroundColor = UIColor.clear 
      return names.count 

    } 

    private func tableView(tableView: UITableView,height section: Int)->CGFloat { 
     return cellspacing 
    } 




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 

     let mycell = self.StreamsTableView.dequeueReusableCell(withIdentifier: "prototype1", for: indexPath) as! HomePage_TableViewCell 
     mycell.Fullname.setTitle(names[indexPath.row], for: UIControlState.normal) 
     // Click Event below 
     mycell.Fullname.addTarget(self, action: "Fullname_Click", for: UIControlEvents.touchUpInside) 
      mycell.Fullname.tag = indexPath.row 


     tableView.separatorColor = UIColor.clear 


     return mycell 


    } 


} 

的主要问题是这段代码

func Fullname_Click(){ 
     // Where the # 32 is I would like to replace that with Profile_ID 
      UserDefaults.standard.set("32", forKey: "HomePage_Fullname_ID") 
      let navigate = self.storyboard?.instantiateViewController(withIdentifier: "Profiles") as? MyProfileViewController 
      self.navigationController?.pushViewController(navigate!, animated: true) 
     } 

通知我硬编码数我想的是,以取代32号属于该特定TableView单元的profile_id的值。该PROFILE_ID是在此代码

    if let profile_id = Stream["profile_id"] as? String { 
          self.profile_ids.append(profile_id) 
         } 

我能找到一种方法把它传递到FullName_Click功能访问...

回答

1

你几乎没有访问PROFILE_ID财产self.profile_id 。您只需进行一些小的更改,即可访问该单元中用户的profile_id

  1. 更改选择Fullname_Click的这个

    func Fullname_Click(sender: UIButton) 
    
  2. 签名在cellForRowAtIndexPath:方法这样

    button.addTarget(self, action: #selector(HomePageViewController.Fullname_Click(sender:)), for: .touchUpInside) 
    
  3. 添加选择在Fullname_Click:的implemetation现在你有你的按钮如sender。使用它的标签来获取用户的profile_idprofile_ids阵列这样

    let profile_id = profile_ids[sender.tag] 
    
+0

非常感谢该工作正常 – user1949387

1

解决方案1: 假设,有存在一个PROFILE_ID每个名称,

您可以使用索引路径进行访问。

@IBAction func resetClicked(sender: AnyObject) { 
let row = sender.tag 
let pid = self.profile_ids[row] 
UserDefaults.standard.set(pid, forKey:"HomePage_Fullname_ID") 
// rest of the code 
} 

解决方案2: 假设你有一个单独的自定义单元格,Hom​​ePage_TableViewCell, 创建另一个属性在您的自定义单元格 'PROFILE_ID' HomePage_TableViewCell

内的cellForRowAtIndexPath,设置相应的配置文件ID。

mycell.profile_id = self.profile_ids [indexpath.row]

和移动自定义单元格内的按钮操作,因此您可以

@IBAction func resetClicked(sender: AnyObject) { 
    UserDefaults.standard.set(self.profile_id, forKey:"HomePage_Fullname_ID") 
     // rest of the code 
} 
+0

感谢你,奋力 – user1949387