2016-05-23 6 views
0

Ich bin sehr neu in Swift-Codierung. Ich habe Erfahrung in Android. Also ich weiß, wie kann ich das tun ist Andorid. Es ist wie unten;Custom TableView Parameter inSwift senden

public class MyUserProfileFragment extends BaseFragment { 
    MyUserProfileAdapter adapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     adapter = new MyUserProfileAdapter(MyUserProfileFragment.this, getActivity()); 
    } 
    public showDialog(){ 
     //do something 
    } 
} 

public class MyUserProfileAdapter extends BaseAdapter { 
    MyUserProfileFragment fragmentMyUserProfile; 
    Context context; 
    public MyUserProfileAdapter(MyUserProfileFragment fragment, Context context) { 
     this.context = context; 
     LayoutInflater layoutInflater = LayoutInflater.from(context); 
     this.fragmentMyUserProfile = fragment; 
    } 
    fragmentMyUserProfile.showDialog(); 
} 

So kann ich alle Methoden in MyUserProfileFragment von MyUserProfileAdapter verwenden. Aber ich kann das nicht in Swift tun. In Swift habe ich;

class ViewControllerNewGame: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var tableView: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as! MyCustomCell 
     return cell 
    } 
    func showDialog(){ 
     //some actions 
    } 
} 

class MyCustomCell: UITableViewCell { 
    @IBOutlet weak var img1: UIButton! 
    @IBAction func click(sender: UIButton) { 
     //I want to use showDialog method from ViewVoncontroller 
    } 
} 

Das sind sehr einfache Codes aus meinem Projekt. Wie kann ich die showDialog-Methode in Swift verwenden?

Antwort

2

Verwenden Sie diese Methode in Viewcontroller:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewControllerNewGame.click(_:)),name:"click", object: nil) 

func show(notification: NSNotification) { 
     ViewControllerNewGame.click() 
} 

Und verwenden Sie diese Methode in MyCustomCell:

NSNotificationCenter.defaultCenter().postNotificationName("click", object: nil) 
+0

Oh vielen Dank, ich bin jetzt sehr glücklich :) – Aybuke

+0

Gern geschehen! :) –