无法从UITableViewCell调用performseguewithidentifier

时间:2017-03-21 04:04:48

标签: swift

我无法在Infinite3TableViewCell类里面调用performseguewithidentifier。我知道它是UITableViewCell。 那么,如何在Infinite3TableViewCell中调用performseguewithidentifier方法?

我想选择一个单元格然后显示/ segue到新的ViewController。

// MARK: - UITableViewDataSource,UITableViewDelegate final class BuyTableViewController:UIViewController {

@IBOutlet weak var tableView: UITableView! {
    didSet {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
    }
}

// MARK: - UITableViewDataSource,UITableViewDelegate 扩展BuyTableViewController:UITableViewDataSource,UITableViewDelegate {

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()
}


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


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0, 1, 2, 3, 4: return 1
    default: return 0
    }
}


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

    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "Infinite0TableViewCell", for: indexPath)
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: InfiniteTableViewCell.identifier) as! InfiniteTableViewCell
        return cell
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: Infinite2TableViewCell.identifier) as! Infinite2TableViewCell
        return cell
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: Infinite3TableViewCell.identifier) as! Infinite3TableViewCell
        return cell
    case 4:
        let cell = tableView.dequeueReusableCell(withIdentifier: Infinite4TableViewCell.identifier) as! Infinite4TableViewCell
        return cell
    default:
        return UITableViewCell()
    }
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch indexPath.section {
    case 0: return 80
    case 1: return 150
    case 2: return 120
    case 3: return 400
    case 4: return 500
    default: return 0
    }

final class CollectionViewCell:UICollectionViewCell {

@IBOutlet weak var imgImage: UIImageView!

@IBOutlet weak var lblName: UILabel!

@IBOutlet weak var imgEffect: UIVisualEffectView!

final class Infinite3TableViewCell:UITableViewCell {

static let identifier = "Infinite3TableViewCell"


var nameArray = ["Women's Clothing", "Beauty & Personl Care", "Women's Bags", "Men's Clothing", "Mobile & Gadgets", "Watches", "Home", "Toys", "Home Appliances"]
var imgName = [UIImage(named: "girl"), UIImage(named: "makeup"), UIImage(named: "bags"), UIImage(named: "clothing"), UIImage(named: "mobile"), UIImage(named: "watches"), UIImage(named: "living"), UIImage(named: "toys"), UIImage(named: "tools")]

扩展Infinite3TableViewCell:UICollectionViewDelegate,UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{

    return nameArray.count

}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{



    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! CollectionViewCell
    cell.imgImage.image = imgName[indexPath.row]
    cell.lblName.text! = nameArray[indexPath.row]
    return cell


}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    print("didSelectItemAt: \(indexPath.item)")

1 个答案:

答案 0 :(得分:0)

应该从performseguewithidentifier或其子类调用

UIViewController。您应该让UIViewController知道并致电它。