为什么委托在UIVewController中工作正常但在swift中没有在TableViewCell中工作?

时间:2017-11-22 05:53:42

标签: ios swift

我有两个委托,其中一个在我的tableview中展开或折叠一个单元格,其中一个必须在tableViewCell类中打印一些东西进行测试。我的代码如下:

protocol ExpandableHeaderViewDelegate {
func toggleSection(header: ExpandableHeaderView, section: Int)
}

protocol MenuCellDelegate {
func menuCellDidChange()
}

class ExpandableHeaderView: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var delegate2 : MenuCellDelegate?

var section: Int!

override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action:     #selector(selectHeaderAction)))
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer)          {
let cell = gestureRecognizer.view as! ExpandableHeaderView
delegate2?.menuCellDidChange()
delegate?.toggleSection(header: self, section: cell.section)
}

func setDelegate2(delegate2: MenuCellDelegate)  {
self.delegate2 = delegate2
}
func customInit(title: String, section: Int, delegate:     ExpandableHeaderViewDelegate,width : Int,height: Int) {
    let cell = Bundle.main.loadNibNamed("MenuTableViewCell", owner: self, options: nil)?.first as! MenuTableViewCell
    let menucell = DataService.instance.getMenuCells()[section]
    if (section==3){
        cell.updateViewsWithFont(menuCell: menucell)
    }else{
        cell.updateViews(menuCell: menucell)
    }
    self.addSubview(cell)
    cell.frame.size.width = CGFloat(width)
    cell.frame.size.height = CGFloat(height / 8)

    self.section = section

self.delegate = delegate
}

override func layoutSubviews() {
super.layoutSubviews()
}

}

和我的tableViewCell:

class MenuTableViewCell: UITableViewCell,MenuCellDelegate {


@IBOutlet weak var menuCellIcon: UIImageView?
@IBOutlet weak var menuCellLabel: UILabel?
@IBOutlet weak var fontAwsomeLabel: SwiftIconLabel?

let delegateTest = ExpandableHeaderView()

func updateViews(menuCell:MenuCell)  {
delegateTest.setDelegate2(delegate2: self)
menuCellIcon?.image = UIImage(named: menuCell.menuCellIcon)
menuCellLabel?.text = menuCell.menuCellLabel
}

func menuCellDidChange() {
fontAwsomeLabel?.font = UIFont.icon(from: .FontAwesome, ofSize: 30.0)
fontAwsomeLabel?.text = String.fontAwesomeIcon("angledown")
print("yyyy")
}


func updateViewsWithFont(menuCell:MenuCell) {
menuCellIcon?.image = UIImage(named: menuCell.menuCellIcon)
menuCellLabel?.text = menuCell.menuCellLabel
fontAwsomeLabel?.font = UIFont.icon(from: .FontAwesome, ofSize: 30.0)
fontAwsomeLabel?.text = String.fontAwesomeIcon("angleleft")

}

}

和我的viewController:

class HomeVC:    UIViewController,UITableViewDelegate,UITableViewDataSource,ExpandableHeaderVi    ewDelegate,MenuCellDelegate {
func menuCellDidChange() {
print("eeeeeeeeee")
}




@IBOutlet weak var homeMenuTableView: UITableView!

var sections = [
MenuCell(menuCellIcon: "ico_news.png",menuCellLabel:"اطلاع رسانی",     menuSubCells: [], expanded: false),
MenuCell(menuCellIcon:"ico_filter.png",menuCellLabel:"دروازه بانی",menuSubCells: [], expanded: false),
MenuCell(menuCellIcon:"ico_sharing.png",menuCellLabel:"اشتراک گذاری خبر",menuSubCells: [], expanded: false),
MenuCell(menuCellIcon:"ico_massaging.png",menuCellLabel:"آرشیو پیام ها",
         menuSubCells:     [MenuCell(menuCellIcon:"ico_inbox.png",menuCellLabel:"آرشیو پیام",     menuSubCells: [], expanded: false),
                            MenuCell(menuCellIcon:"ico_compose.png",menuCellLabel:"پیام جدید", menuSubCells: [], expanded: false)], expanded: false),
MenuCell(menuCellIcon:"ico_massaging.png",menuCellLabel:"تنظیمات",menuSubCells: [], expanded: false)
]


override func viewDidLoad() {
super.viewDidLoad()
        self.homeMenuTableView.backgroundColor = UIColor(red: 0.0196,     green: 0.1059, blue: 0.1647, alpha: 1.0)
        let px = 1 / UIScreen.main.scale
        let frame = CGRect(x:0,y:0,width:     self.homeMenuTableView.frame.size.width,height: px)
        let line = UIView(frame: frame)
        self.homeMenuTableView.tableHeaderView = line
        line.backgroundColor = self.homeMenuTableView.separatorColor
        homeMenuTableView.tableFooterView = UIView()
        homeMenuTableView.dataSource=self
        homeMenuTableView.delegate=self
}

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].menuSubCells.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return homeMenuTableView.frame.size.height / 8
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (sections[indexPath.section].expanded) {
    return homeMenuTableView.frame.size.height / 8
}
return 0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.setDelegate2(delegate2: self)
header.customInit(title: sections[section].menuCellLabel, section:  section, delegate: self,width : Int(homeMenuTableView.frame.width),height :  Int(homeMenuTableView.frame.size.height))
return header
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("MenuTableViewCell", owner: self,  options: nil)?.first as! MenuTableViewCell
let menucell = sections[indexPath.section].menuSubCells[indexPath.row]
cell.updateViews(menuCell: menucell)
return cell
}

func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
homeMenuTableView.beginUpdates()
for i in 0 ..< sections[section].menuSubCells.count {
    homeMenuTableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
homeMenuTableView.endUpdates()
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor(red: 0.0196, green: 0.1059, blue: 0.1647, alpha: 1.0)
}


}

问题是viewController中的menuCellDidChange工作正常,但MenuTableViewCell中的menuCellDidChange不起作用。

2 个答案:

答案 0 :(得分:1)

您的代码中存在问题。这两者与您面临的问题有些相关:

  1. 您没有为代表使用弱指针。这将导致内存泄漏。

  2. MenuTableViewCell中,您要创建ExpandableHeaderView的新实例,此处为let delegateTest = ExpandableHeaderView()。如果这个新的标题视图实例没有附加到视图,可以在点击手势操作上调用委托吗?

  3. 在我看来,您需要学习如何使用委托,弱指针和强指针以及视图层次结构。网上有大量的源材料。在完成这些基本概念之后,您将看到为什么您的代码无效。

答案 1 :(得分:0)

当您为let delegateTest = ExpandableHeaderView()编写ExpandableHeaderView()时,为每个单元格创建_weak var delegateTest: ExpandableHeaderView? 的新实例。因此委托将被调用,但不会在您希望被调用的实例上调用。

而不是你应该在单元格中创建一个变量

cellForRowAtIndexPath

并在cell.delegateTest = self 中为委托变量赋值,如下所示

{{1}}