Ios 在swift中,如何将tableview委托方法从一个类调用到另一个类?
我想为每个单元格执行下拉操作,因此我在单元格中插入了一个tableview,并且我正在使用单元格单击时展开的展开单元格,以便它显示为下拉列表 MainTableView:Ios 在swift中,如何将tableview委托方法从一个类调用到另一个类?,ios,swift,uitableview,delegates,Ios,Swift,Uitableview,Delegates,我想为每个单元格执行下拉操作,因此我在单元格中插入了一个tableview,并且我正在使用单元格单击时展开的展开单元格,以便它显示为下拉列表 MainTableView: import UIKit class MainTableView: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var mainTableView: UITableView! var selectedCellInde
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:
导入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
}
}
如果我在mainTableViewCell类中调用mainTableView委托方法,那么这将很容易,但我正在努力解决这个问题。帮帮我 首先创建单元格的委托方法:-
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()
}
}
然后在MainTableView中添加MainTableViewCellDelegate:-
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
}
}
首先创建单元格的委托方法:-
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()
}
}
然后在MainTableView中添加MainTableViewCellDelegate:-
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
}
}
使用postNotification方法或自定义委托方法,我尚未阅读所有代码,但是,如果您想调用
MainTableView
,如何为MainTableViewCell
提供一个设置为self
的属性(即主控制器)在顶级cellForRowAtIndexPath
?使用postNotification方法或自定义委托方法时,我还没有阅读所有代码,但是,如果您想调用MainTableView
,给MainTableViewCell
一个设置为self
的属性(即主控制器)如何在顶层单元浏览索引路径过程中,您会告诉我如何使用另一个表视图单元中的表视图执行下拉吗?为此,您应该阅读第三方库或一些教程。您会告诉我如何使用另一个表视图单元中的表视图执行下拉吗?为此你应该通过第三方图书馆或一些教程。