首页 >社区问答列表 >ios - 自定义的一个UITableView delegate什么的都设置了 但是那几个代理方法没有执行

ios - 自定义的一个UITableView delegate什么的都设置了 但是那几个代理方法没有执行

我创建一个tableView,cell使用xib写的。接口也都用了,delegate和datasource都没忘记。但是tableView是有,但是numberOfRowsInSection这个代理执行了,cellForRowAtIndexPath却没有执行,这是为什么。网上主要查到三种说法,一个是delegate = self什么的没写,但是这些我写了,一个是numberOfRowsInSection返回的是0,这个我这里直接返回了一个3。还有一种是cell的auto layout没弄好,我设置了好几次也还是没显示。实在无计可施了。
import UIKit

class UnDoneViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

let width = UIScreen.mainScreen().bounds.width
let heigth = UIScreen.mainScreen().bounds.height
var unDoneTable:UITableView!    //未付款订单列表


override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.whiteColor()
    self.createUnDoneTable()
}

//添加未付款订单列表
func createUnDoneTable(){
    unDoneTable = UITableView()
    unDoneTable.frame = CGRect(x: 0, y: 153, width: width, height: heigth)
    unDoneTable.delegate = self
    unDoneTable.dataSource = self
    unDoneTable.rowHeight = 116
    unDoneTable.backgroundColor = UIColor(red: 249/255, green: 250/255, blue: 250/255, alpha: 1)
    self.view.addSubview(unDoneTable)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    print("22222")
    return 3
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var cell = tableView.dequeueReusableCellWithIdentifier("UnDoneTableViewCell") as? UnDoneTableViewCell
    if (cell == nil) {
        let nibs:NSArray = NSBundle.mainBundle().loadNibNamed("UnDoneTableViewCell", owner: self, options: nil)
        cell = nibs.lastObject as? UnDoneTableViewCell
        cell?.selectionStyle = .None
    }
    print("11111")
    return cell!
}


}

  • 伊谢尔伦
  • 伊谢尔伦    2017-04-17 17:48:272楼

    因为 numberOfRowsInSection 返回3
    所以 cellForRowAtIndexPath 中, 改为:

    if (cell == nil) {
            let nibs:NSArray = NSBundle.mainBundle().loadNibNamed("UnDoneTableViewCell", owner: self, options: nil)
            cell = nibs.lastObject as? UnDoneTableViewCell
        }
    
    cell?.selectionStyle = .None

    +0添加回复

  • 回复