2017-09-29 4 views
0

セルを別のビューに移動するように設定する方法を見つけようとしています。この場合、グループをリストしていますログイン後、ユーザーが好きなサービスをタップすると、それらはマップに移動します。しかし、タップしたときに地図に表示されるようにセルを設定する方法はわかりません。私はセグを作成しようとしましたが、セルがタップされても何も起こりません。私はプログラミングが初めてで、誰かがこれを説明できるかどうか疑問に思っていました。私は、セルをXcode 9の別のビューコントローラに移動する方法を知りました。

私はセル(基本的なもの)を設定する方法の理解を私に与えたYouTubeビデオの束を見ました。

本当にありがとうございます、ありがとうございました!

この投稿は、プログラミングの旅に足を浸している人を支援します。

ありがとう、幸せなコーディング!ここで

は、私が現在持っているコードです:

import UIKit 
struct cellData { 
let cell : Int! 
let text : String! 
let image : UIImage! } 

class ListServicesTVC: UITableViewController { 

var arrayOfCellData = [cellData]() 


override func viewDidLoad() { 
    arrayOfCellData = [cellData(cell : 1, text : "Barber Services", image : #imageLiteral(resourceName: "barberservice")), 
         cellData(cell : 2, text : "Salon Services", image : #imageLiteral(resourceName: "salonservice"))] 
} 

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

    return arrayOfCellData.count 

} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if arrayOfCellData[indexPath.row].cell == 1 { 

     let cell = Bundle.main.loadNibNamed("BarberServiceCell", owner: self, options: nil)?.first as! BarberServiceCell 

     cell.barberImageView.image = arrayOfCellData[indexPath.row].image 
     cell.barberServicesLabel.text = arrayOfCellData[indexPath.row].text 

     return cell 
    } 
    else if arrayOfCellData[indexPath.row].cell == 2 { 

     let cell = Bundle.main.loadNibNamed("SalonServicesCell", owner: self, options: nil)?.first as! SalonServicesCell 

     cell.salonImageView.image = arrayOfCellData[indexPath.row].image 
     cell.salonServicesLabel.text = arrayOfCellData[indexPath.row].text 

     return cell 
    } 

    else { 
     let cell = Bundle.main.loadNibNamed("BarberServiceCell", owner: self, options: nil)?.first as! BarberServiceCell 

     cell.barberImageView.image = arrayOfCellData[indexPath.row].image 
     cell.barberServicesLabel.text = arrayOfCellData[indexPath.row].text 

     return cell 
    } 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    if arrayOfCellData[indexPath.row].cell == 1 { 
     return 120 
    } 
    else if arrayOfCellData[indexPath.row].cell == 2 { 
     return 120 
    } 
    else { 
     return 120 
    } 

} 
} 
+0

'cell = Bundle.main.loadNibNamed(" BarberServiceCell "、owner:self、options:nil)と書かないでください。 BarberServiceCell'。それは細胞の「再使用」を避けます。代わりに 'tableView.dequeueCell ...'を使います。タップを検出するには、 'UITableViewDelegate'メソッド' didSelect: 'を実装する必要があります – Larme

+0

*セル*(ビュー)を別のビューに移動して*データソース項目*(モデル) – vadian

+0

フルラインコードの外観 – Adeel

答えて

0

を:

  • のViewControllerクラスでのtableViewアウトレットを作成。
  • TableViewCellクラスを作成し、tableView Outletに登録します。メインで
  • その後、DetailViewControllerクラスを作成します(つまり、あなたが特定のセルをクリックすると、それはその特定のセルの内容を表示する必要があります)

「のViewControllerは、」「次のコードに

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {を行います

@IBOutlet var tableView: UITableView! 


var tableData: [String] = ["Apple", "Samsung", "LG"] 

// 1 
override func viewDidLoad() { 
    super.viewDidLoad() 


// Register customCell with tableView Outlet 
    let nib = UINib(nibName: "CustomTableViewCell", bundle: nil) 
    tableView.registerNib(nib, forCellReuseIdentifier: "cell") 

} 

// 2 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.tableData.count 
} 

// 3 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  { 

    let cell: CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomTableViewCell 
    // injecting data to cell 
    cell.lblCompanyName.text = tableData[indexPath.row] 
    cell.imgCompanyName.image = UIImage(named: tableData[indexPath.row]) 

    return cell 

} 

// 4 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


    let detailObj=DetailViewController(nibName: "DetailViewController", bundle: nil) 
    self.navigationController?.pushViewController(detailObj, animated: true) 
    detailObj.nameVar=tableData[indexPath.row] 
    detailObj.imgStr=tableData[indexPath.row] 

} 
"CustomTableViewCell" クラスにおいて
  • クラスCustomTableViewCell:のUITableViewCell {

    @IBOutlet var imgCompanyName: UIImageView! 
    @IBOutlet var lblCompanyName: UILabel! 
    
    
    override func awakeFromNib() { 
        super.awakeFromNib() 
        // Initialization code 
    }} 
    
  • "DetailViewController"
  • クラスDetailViewControllerで:コードののUIViewController {

    @IBOutlet var name: UILabel! 
        @IBOutlet var image: UIImageView! 
        var nameVar:String? 
        var imgStr:String? 
        override func viewDidLoad() { 
    
         name.text=nameVar 
         image.image=UIImage(named: imgStr!) 
    
        super.viewDidLoad() 
    
        // Do any additional setup after loading the view. 
    }} 
    

    エンド

    私はあなたが下のコメントにちょうどコメントを持っているならば、私は明らかだと思います。

    -1

    こんにちは以下のコードセットを試してみてください、私はそれがあなたの解決を願っています詳細を渡すために必要である、あなたのコード内のいくつかの追加の変更を、追加しました問題。
    私はあなたが第二のクラスで

    class ListServicesTVC: UITableViewController { 
    
    // Add this variable in this class and use it whereever you needed it in this class 
    var selectedItem: cellData? 
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
        // Now maintain the selected data in the local variable we declared 
        selectedItem = arrayOfCellData[indexPath.row] 
    
        // Now perform the segue operation 
        performSegue(withIdentifier: "VIEW_CONTROLLER_IDENTIFIER_OF_MAP_CLASS", sender: self) 
    } 
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
        if segue.identifier == "VIEW_CONTROLLER_IDENTIFIER_OF_MAP_CLASS" { 
         let destinationVC = segue.destination as? VIEW_CONTROLLER_IDENTIFIER_OF_MAP_CLASS 
         destinationVC?.selectedItem = self.selectedItem // Pass the selected item here which we have saved on didSelectRotAt indexPath delegate 
        } 
    } 
    

    を必要なだけ追加のコード追加しました:ただ、以下の手順に従ってください

    class VIEW_CONTROLLER_IDENTIFIER_OF_MAP_CLASS: UIViewController { 
    
    
    // Add this variable in this class and use it whereever you needed it in this class 
    var selectedItem: cellData?