2016-05-30 7 views
1

Ich möchte eine Dropdown für jede Zelle durchführen, daher habe ich eine Tabellenansicht mit in eine Zelle eingefügt und ich arbeite mit der expandierenden Zelle, die eine Zelle erweitert wird, wenn es klickt, so dass es als Dropdown angezeigt wird.Wie kann ich eine tableview Delegate Methoden von einer Klasse in eine andere Klasse in swift aufrufen?

MainTableView:

import UIKit 

class MainTableView: UIViewController,UITableViewDelegate,UITableViewDataSource { 

@IBOutlet weak var mainTableView: UITableView! 
var selectedCellIndexPath: NSIndexPath? 
let selectedCellHeight: CGFloat = 300.0 
let unselectedCellHeight: CGFloat = 44.0 
var name = ["red","green","blue","white"] 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return name.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = mainTableView.dequeueReusableCellWithIdentifier("cell1" , forIndexPath: indexPath) as! mainTableViewCell 
    cell.Title.text = name[indexPath.row] 
    return cell 
} 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if selectedCellIndexPath == indexPath { 
     return selectedCellHeight 
    } 
    return unselectedCellHeight 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath { 
     selectedCellIndexPath = nil 
    } else { 
     selectedCellIndexPath = indexPath 
    } 

    tableView.beginUpdates() 
    tableView.endUpdates() 

    if selectedCellIndexPath != nil { 
     // This ensures, that the cell is fully visible once expanded 
     tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true) 
    } 
} 

} 

MainTableViewCell:

import UIKit 

class MainTableViewCell:UITableViewCell,UITableViewDataSource,UITableViewDelegate { 

@IBOutlet weak var dropDownList: UITableView! 
@IBOutlet weak var Title: UILabel! 
var selectedCellIndexPath: NSIndexPath? 
let selectedCellHeight: CGFloat = 0.0 
let unselectedCellHeight: CGFloat = 44.0 
var name = ["magenta","yellow","orange","cyan","black","grey"] 
override func awakeFromNib() { 
    super.awakeFromNib() 

    dropDownList.delegate = self 
    dropDownList.dataSource = self 
} 

override func setSelected(selected: Bool, animated: Bool) 
{ 
    super.setSelected(selected, animated: animated) 
} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return name.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = dropDownList.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! subTableViewCell 
    cell.subTitle.text = name[indexPath.row] 
    return cell 
} 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if selectedCellIndexPath == indexPath { 
     return selectedCellHeight 
    } 
    return unselectedCellHeight 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    /* If I select any row in subTableView then the tableview has to disappear and the height of mainTableView row should be rearranged(i.e the dropdown should dissappear) */ 

} 

InnerTableViewCell: Import UIKit

class subTableViewCell: UITableViewCell { 

@IBOutlet weak var subTitle: UILabel! 
override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

Wenn ich die mainTableView Delegatmethoden in der mainTableViewCell Klasse aufrufen, dann wäre es leicht, aber ich habe damit zu kämpfen p ich!

+0

Verwenden postNotification Methode oder benutzerdefinierte Delegatmethoden, –

+0

Ich habe nicht gelesen den ganzen Code aber, wenn Sie zurück zum 'MainTableView' aufrufen möchten, wie wäre es mit dem' MainTableViewCell' eine Eigenschaft, die auf 'self' (dh den Hauptcontroller) während der obersten Ebene' cellForRowAtIndexPath' gesetzt wird? –

Antwort

0

Zuerst erstellen Delegatmethode zu Zelle: -

import UIKit 

protocol MainTableViewCellDelegate { 
    func myFunc() 
} 

class MainTableViewCell:UITableViewCell,UITableViewDataSource,UITableViewDelegate { 

    var MainTableViewCellDelegate! = nil 

    // Put Your Other code Lines 


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     delegate.myFunc() 
    } 
} 

Dann in Ihrem MainTableView MainTableViewCellDelegate hinzufügen: -

import UIKit 

class MainTableView: UIViewController,UITableViewDelegate,UITableViewDataSource, MainTableViewCellDelegate { 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = dropDownList.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! subTableViewCell 
      cell.subTitle.text = name[indexPath.row] 
      cell.delegate = self 
      return cell 
     } 

     func myFunc() { 
      self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil) //Put your segue 
     } 
} 
+0

Können Sie angeben, wie Dropdown mit einer Tabellenansicht ausgeführt wird, die sich in der Zelle einer anderen Tabellenansicht befindet? – GuganReddy

+0

Dafür sollten Sie mit einer 3rd-Party-Bibliothek oder einem Tutorial durchgehen. –

Verwandte Themen