【问题标题】:How can I call a tableview delegate methods from one class to another class in swift?如何快速从一个类调用 tableview 委托方法到另一个类?
【发布时间】:2016-09-28 06:41:11
【问题描述】:

我想为每个单元格执行一个下拉菜单,因此我在一个单元格中插入了一个表格视图,并且我正在使用扩展单元格,当它单击时单元格会被展开,以便它显示为一个下拉列表。

主表视图:

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 委托方法,那会很容易,但我对此感到很困惑。帮帮我!

【问题讨论】:

  • 使用 postNotification 方法或自定义委托方法,
  • 我还没有阅读所有代码但是,如果你想回调MainTableView,如何给MainTableViewCell一个设置为self的属性(即主控制器)在顶层cellForRowAtIndexPath?

标签: ios swift uitableview delegates


【解决方案1】:

首先为单元格创建一个委托方法:-

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

【讨论】:

  • 您能告诉我们如何使用另一个表格视图单元格内的表格视图执行下拉菜单吗?
  • 为此,您应该使用 3rd 方库或一些教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2015-10-02
  • 2017-09-15
相关资源
最近更新 更多