2016-05-23 42 views
0

我很新的Swift编码。我有Android的经验。所以我知道我该怎么做Andorid。它如下所示;自定义TableView发送参数inSwift

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

所以我可以使用MyUserProfileAdapter的MyUserProfileFragment中的所有方法。 但我不能在Swift中做到这一点。在斯威夫特我有;

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

这些都是我的项目中非常类似的代码。我如何在Swift中使用showDialog方法?

回答

2

使用此方法的ViewController:

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

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

,并使用此方法在MyCustomCell

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

哦,非常感谢你,我现在很开心:) – Aybuke

+0

您的欢迎! :) –

相关问题