简体   繁体   English

将自定义tableviewcell连接到Uitableview

[英]connect custom tableviewcell to Uitableview

I am new in programming so I really need ur help. 我是编程新手,所以我真的需要你的帮助。 Basically I have very basic table view controller with custom cell inside. 基本上我有非常基本的表视图控制器,里面有自定义单元。

import UIKit

class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]

var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"]

var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]

var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return restaurantNames.count
}

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

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

    // Configure the cell...
    cell.nameLabel.text = restaurantNames[(indexPath as NSIndexPath).row]
    cell.thumbnailImageView.image = UIImage(named: restaurantImages[(indexPath as NSIndexPath).row])
    cell.locationLabel.text = restaurantLocations[(indexPath as NSIndexPath).row]
    cell.typeLabel.text = restaurantTypes[(indexPath as NSIndexPath).row]


    return cell

Also i have 4 sep table view controllers. 我还有4个sep表视图控制器。 My question is how can I establish segues so when I press for example the first cell - "a" tableviewcontroller opened. 我的问题是如何建立segues所以当我按下第一个单元格时 - “a”tableviewcontroller打开。 When I pressed second cell -"b"tableviewcontroller opened. 当我按下第二个单元格 - “b”tableviewcontroller打开。

I am very very new in programming, so please guys,write everything step by step. 我是非常非常新的编程,所以请大家一步一步写下所有内容。 Thank you so much for the HELP!!! 非常感谢你的帮助!!!

you can use UITableViewDelegate Methods like this 你可以像这样使用UITableViewDelegate方法

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        switch indexPath.row {
        case 0:
            //open first view controller
            print("First row Click")
        case 1:
        //open Second view controller
            print("Second row Click")
        default:
             print("Nothing")
        }


    }

step 1: Connect all the segues from view controller and give segue Identifier to each connection. 步骤1:连接视图控制器中的所有segue,并为每个连接提供segue Identifier。

see the how to connect and give ID : http://imgur.com/a/Ebm4V 请参阅如何连接并提供ID: http//imgur.com/a/Ebm4V

step 2: write this code in didSelectRowAtIndexPath method. 第2步:在didSelectRowAtIndexPath方法中编写此代码。

import UIKit

class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]

var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"]

var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]

var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return restaurantNames.count
}

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

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

    // Configure the cell...
    cell.nameLabel.text = restaurantNames[(indexPath as NSIndexPath).row]
    cell.thumbnailImageView.image = UIImage(named: restaurantImages[(indexPath as NSIndexPath).row])
    cell.locationLabel.text = restaurantLocations[(indexPath as NSIndexPath).row]
    cell.typeLabel.text = restaurantTypes[(indexPath as NSIndexPath).row]


    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let segID1 = “first viewcontroller ID”
    let segID2 = “second viewcontroller ID”
    let segID3 = “third viewcontroller ID”
    let segID4 = “fourth viewcontroller ID”

    if indexPath.row == 0{
        self.performSegueWithIdentifier(segID1, sender: self)
    }else if indexPath.row == 1{
        self.performSegueWithIdentifier(segID2, sender: self)
    }else if indexPath.row == 2{
        self.performSegueWithIdentifier(segID3, sender: self)
    }else if indexPath.row == 3{
        self.performSegueWithIdentifier(segID4, sender: self)
    }
}

}

The other responses are developed for Swift2 but you want it for Swift3 that's why you can't find the methods. 其他响应是为Swift2开发的,但是你想要它用于Swift3,这就是你无法找到方法的原因。

So I am going to provide you a very basic example of navigation between TableViewControllers hopefully it can help you to understand how the navigation works so that you implement it in your project. 因此,我将为您提供TableViewControllers之间导航的一个非常基本的示例,希望它可以帮助您了解导航的工作原理,以便您在项目中实现它。

Result 结果

Code

class TableViewController: UITableViewController {
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "Go to Table \(indexPath.row + 1)"
    return cell
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let numberOfRow = indexPath.row

    switch numberOfRow{
    case 0:
      performSegue(withIdentifier: "showA", sender: numberOfRow)
    case 1:
      performSegue(withIdentifier: "showB", sender: numberOfRow)
    default:
      break;
    }
  }

  // This method will be useful to you
  // in case you want to send any info
  // to your destination view controllers.
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showA" {
      let tableViewControllerA = segue.destination as! TableViewControllerA
      tableViewControllerA.someData = sender as? Int
    } else if segue.identifier == "showB" {
      let tableViewControllerB = segue.destination as! TableViewControllerB
      tableViewControllerB.someData = sender as? Int
    }
  }
}

class TableViewControllerA: UITableViewController {
  var someData: Int?

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Table 1"
  }
}

class TableViewControllerB: UITableViewController {
  var someData: Int?

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Table 2"
  }
}

Additional Info: 附加信息:

在此输入图像描述


The way you do it is like below: 你这样做的方式如下:

在此输入图像描述


在此输入图像描述


在此输入图像描述


在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM