最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中的UITableView的UITableViewDelegate和UITableViewDataSource不生效

iOS crifan 2818浏览 0评论

折腾:

[已解决]swift实现点击按钮出现下拉菜单

期间,用代码:

        let addDropdownWidth:CGFloat = 200

        let addDropdownFrame = CGRectMake(

            //UIScreen.mainScreen().bounds.width – addDropdownWidth,

            self.view.bounds.width – addDropdownWidth,

            STATUS_BAR_HEIGHT + NAVI_BAR_HEIGHT,

            addDropdownWidth,

            ADD_DROPDOWN_LIST_CELL_HEIGHT * 4)

        //print("addDropdownFrame=\(addDropdownFrame)")

        //addDropdownTableView = UITableView(frame: addDropdownFrame)

        addDropdownTableView = UITableView()

        addDropdownTableView.frame = addDropdownFrame

        addDropdownTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

        addDropdownTableView.backgroundColor = UIColor.yellowColor()

        //addDropdownTableView.alpha = 0.8

        addDropdownTableView.hidden = true

       

        let addDropdownListVC = AddDropdownListViewController()

        addDropdownTableView.delegate = addDropdownListVC

        addDropdownTableView.dataSource = addDropdownListVC

//        addDropdownTableView.delegate = self

//        addDropdownTableView.dataSource = self

       

        //self.addChildViewController(addDropdownListVC)

       

        //addDropdownTableView.reloadData()

       

//        addDropdownTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

       

        self.view.addSubview(addDropdownTableView)

 然后:

//

//  AddDropdownListViewController.swift

//  JianDao

//

//  Created by licrifan on 15/11/4.

//  Copyright © 2015年 licrifan. All rights reserved.

//

import UIKit

class AddDropdownListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

//class AddDropdownListViewController: UITableViewController{

    let dropdownListTitleArr = ["发起会话", "发起话题", "添加同事", "添加人脉"]

   

//    var addDropdownTableView:UITableView!

//   

     override func viewDidLoad() {

        super.viewDidLoad()

//       

//        let addDropdownWidth:CGFloat = 200

//        let addDropdownFrame = CGRectMake(

//            //UIScreen.mainScreen().bounds.width – addDropdownWidth,

//            self.view.bounds.width – addDropdownWidth,

//            STATUS_BAR_HEIGHT + NAVI_BAR_HEIGHT,

//            addDropdownWidth,

//            ADD_DROPDOWN_LIST_CELL_HEIGHT * 4)

//        print("addDropdownFrame=\(addDropdownFrame)")

//       

//        addDropdownTableView = UITableView(frame: addDropdownFrame)

//        addDropdownTableView.delegate = self

//        addDropdownTableView.dataSource = self

//       

//        addDropdownTableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine

//        addDropdownTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

//        addDropdownTableView.backgroundColor = UIColor.blueColor()

//        addDropdownTableView.alpha = 0.6

//       

//       

//        addDropdownTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

//        self.view.addSubview(addDropdownTableView)

       

//        self.registerClass(UITableViewCell.self, forCellReuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

    }

   

   

    /***************************************************************************

     * UITableViewDataSource functions

     ***************************************************************************/

   

//    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

//        return 1

//    }

   

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //print("numberOfRowsInSection indexPath = \(indexPath)")

        print("dropdownListTitleArr.count=\(dropdownListTitleArr.count)")

        return dropdownListTitleArr.count

    }

   

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        //print("heightForRowAtIndexPath indexPath = \(indexPath)")

        return ADD_DROPDOWN_LIST_CELL_HEIGHT

    }

   

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        print("cellForRowAtIndexPath indexPath = \(indexPath)")

       

        //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_CHAT)

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

       

        cell.backgroundColor = UIColor.whiteColor()

        cell.selectionStyle = UITableViewCellSelectionStyle.Blue

        cell.accessoryType = UITableViewCellAccessoryType.None

        cell.accessoryView = nil;

       

        cell.detailTextLabel?.text = nil

       

        cell.textLabel?.font = UIFont.boldSystemFontOfSize(14)

        cell.textLabel?.textAlignment = NSTextAlignment.Left

        cell.textLabel?.textColor = UIColor.grayColor()

       

        switch indexPath.row {

        case 0:

            cell.textLabel?.text = dropdownListTitleArr[0]

        case 1:

            cell.textLabel?.text = dropdownListTitleArr[1]

        case 2:

            cell.textLabel?.text = dropdownListTitleArr[2]

        case 3:

            cell.textLabel?.text = dropdownListTitleArr[3]

        default:

            break

        }

       

        return cell

    }

   

    /***************************************************************************

    * UITableViewDelegate functions

    ***************************************************************************/

   

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        //print("didSelectRowAtIndexPath indexPath = \(indexPath)")

       

    }

   

    /***************************************************************************

     * UIViewController functions

     ***************************************************************************/

   

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

       

    }

}

但是结果:

       let addDropdownListVC = AddDropdownListViewController()

        addDropdownView.delegate = addDropdownListVC

        addDropdownView.dataSource = addDropdownListVC

根本没有生效:

显示出来的区域中,根本没有每个cell的标题文字:

后来是在init函数中,添加了:

self.addSubview(addDropdownView)

即可使得delegate和datasource,都生效了。。。

但是还是不行。

搜:

swift UITableViewDataSource not work

swift UITableView delegate DataSource not work

参考:

ios – Swift (some) UITableViewDelegate Methods Not Executing – Stack Overflow

后来经过调试发现:

numberOfRowsInSection

heightForRowAtIndexPath

都执行到了,生效了,但是:

cellForRowAtIndexPath

没有执行到,没有生效。。。

搜:

swift cellForRowAtIndexPath  not work

参考:

swift – UITableView method cellForRowAtIndexPath not called – Stack Overflow

好像是xcode的bug???

ios – Swift: cellForRowAtIndexPath not called in the order of indexPaths – Stack Overflow

swift – cellForRowAtIndexPath is never called – Stack Overflow

swift cellForRowAtIndexPath  not  called

ios – cellForRowAtIndexPath not called but numberOfRowsInSection called – Stack Overflow

说是需要设置frame,啥意思?

cellforrowatindexpath 不执行

我此处没有异步执行,所以不涉及reloadData

cellForRowAtIndexPath不被执行的原因 – 熊猫正正的日志 – 网易博客

TableView的cellForRowAtIndexPath不执行 -CSDN论坛-CSDN.NET-中国最大的IT技术社区

http://bbs.csdn.net/topics/390717148

添加了:

addDropdownTableView.reloadData()

还是没用。

添加了:

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

结果还是不行。

并且,之前的:

numberOfRowsInSection

被执行了两遍,log打印了两次:

dropdownListTitleArr.count=4

dropdownListTitleArr.count=4

而加了numberOfSectionsInTableView后,

numberOfRowsInSection被执行了四次。。。

搜:

numberofrowsinsection called twice

swift numberofrowsinsection called twice

参考:

objective c – UITableView delegate method called twice – Stack Overflow

 添加:

        self.addChildViewController(addDropdownListVC)

结果没用。

iphone – UITableView numberOfRowsInSection called multiple times – Stack Overflow

numberOfSectionsInTableView called twice on UITableView – iPhone Dev SDK

NumberOfRowsInSection called multiple times but cellForRowAtIndexPath never called – www.scriptscoop.net

去试试initWithFrame

发现自己已经是这么做的了:

        addDropdownTableView = UITableView(frame: addDropdownFrame)

NumberOfRowsInSection returns more than 0; cellForRowAtIndexPath still not being called – www.scriptscoop.net

去试试那个registerClass:

加上:

        addDropdownTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

以及更新为:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_CHAT)

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

结果:

问题依旧。。。

最后,实在不行了,只能换用:

把delegate和datasource换成自己:

//class MainViewController: UITabBarController {

class MainViewController: UITabBarController, UITableViewDelegate, UITableViewDataSource {

  override func viewDidLoad() {

        super.viewDidLoad()

        addDropdownTableView.delegate = self

        addDropdownTableView.dataSource = self

}

    /***************************************************************************

     * UITableViewDataSource functions

     ***************************************************************************/

   

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //print("numberOfRowsInSection indexPath = \(indexPath)")

        print("dropdownListTitleArr.count=\(dropdownListTitleArr.count)")

        return dropdownListTitleArr.count

    }

   

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        //print("heightForRowAtIndexPath indexPath = \(indexPath)")

        return ADD_DROPDOWN_LIST_CELL_HEIGHT

    }

   

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        print("cellForRowAtIndexPath indexPath = \(indexPath)")

       

        //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_CHAT)

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: TABLE_CELL_ID_ADD_DROPDOWN_LIST)

       

        cell.backgroundColor = UIColor.whiteColor()

        cell.selectionStyle = UITableViewCellSelectionStyle.Blue

        cell.accessoryType = UITableViewCellAccessoryType.None

        cell.accessoryView = nil;

       

        cell.detailTextLabel?.text = nil

       

        cell.textLabel?.font = UIFont.boldSystemFontOfSize(14)

        cell.textLabel?.textAlignment = NSTextAlignment.Left

        cell.textLabel?.textColor = UIColor.grayColor()

       

        switch indexPath.row {

        case 0:

            cell.textLabel?.text = dropdownListTitleArr[0]

        case 1:

            cell.textLabel?.text = dropdownListTitleArr[1]

        case 2:

            cell.textLabel?.text = dropdownListTitleArr[2]

        case 3:

            cell.textLabel?.text = dropdownListTitleArr[3]

        default:

            break

        }

       

        return cell

    }

}

然后就是可以正常工作的:

cellForRowAtIndexPath

就可以被正常的调用到。。。

转载请注明:在路上 » [已解决]Swift中的UITableView的UITableViewDelegate和UITableViewDataSource不生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.157 seconds, using 22.20MB memory