AddingtblSettings.dataSource = self不加载viewcontroller

时间:2016-05-06 05:34:54

标签: ios swift uitableview uiviewcontroller

我的ViewController类就像这样

import UIKit

class SubscriptionViewController:    
UIViewController,UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tblSettings: UITableView!

@IBOutlet weak var vwTitle: UIView!

let mutableDictionary = ["Business","News","Sports","Entertainment","Crime","Politics"]

var imageView = UIImageView.init(frame: CGRectMake(0, 0, 20, 20))

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
   tblSettings.backgroundColor = UIColor.clearColor()
   self.tblSettings.delegate = self
   self.tblSettings.dataSource = self

}

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 mutableDictionary.count
}

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

    var cell = tableView.dequeueReusableCellWithIdentifier("SettingsCell") as! SettingsTableViewCell!
    if cell == nil
    {
        //tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")
        tableView.registerClass(SettingsTableViewCell.classForCoder(), forCellReuseIdentifier: "SettingsCell")

        cell = SettingsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "SettingsCell")

    }
    cell.backgroundColor=UIColor.clearColor()
    // Custom view options
    let image=UIImage.init(named: "SettingsUnChecked")

    imageView.image=image

    cell.accessoryType = UITableViewCellAccessoryType.None
    cell.accessoryView = imageView//UIButton(frame: CGRectMake(0, 0, 20, 20))

    cell.accessoryView!.backgroundColor = UIColor.clearColor()

    if let label = cell.lblName{
        label.text = mutableDictionary[indexPath.row]
        label.textColor=UIColor.whiteColor()
    }
    return cell

}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

我的问题是我的viewcontroller没有加载。但如果我评论tblSettings.datasource=self我的视图控制器出现了。这是什么原因?请帮我。 感谢

1 个答案:

答案 0 :(得分:0)

可能是因为在您的cellForRowAtIndexPath方法中引用了通用表格视图吗?

尝试:

var cell = self.tblSettings.dequeueReusableCellWithIdentifier("SettingsCell") as! SettingsTableViewCell!