以编程方式使用自定义的uitableviewcell初始化UITableViewController的子类

时间:2015-02-06 15:36:36

标签: ios uitableview swift storyboard

环境:xcode6,swift

我试图使用SLPagingViewSwift作为root控制器,在其中一个页面中,我想使用自定义UITableViewController。

我不知道为什么它不工作,总是扔掉" 2015-02-06 23:03:18.228 xiaoyu [4825:46536]因未捕获的异常而终止应用#39; NSInternalInconsistencyException',原因:'无法使用标识符PersonCell对单元格进行出列 - 必须为标识符注册一个nib或类,或者在故事板中连接原型单元格'"" ,我已经将标识符定义为' PersonCell'在故事情节中。

以下是主要控制者的代码:

var controller: SLPagingViewSwift?

var ctr1 = PersonTableViewController()
var ctr2 = UIViewController()
var ctr3 = UIViewController()

override func viewDidLoad() {
    super.viewDidLoad()


    var navTitleLabel1 = UILabel()
    navTitleLabel1.text = "Home"
    navTitleLabel1.font = UIFont(name: "Helvetica", size: 20)
    navTitleLabel1.textColor = UIColor.whiteColor()
    var navTitleLabel2 = UILabel()
    navTitleLabel2.text = "Discover"
    navTitleLabel2.font = UIFont(name: "Helvetica", size: 20)
    navTitleLabel2.textColor = UIColor.whiteColor()
    var navTitleLabel3 = UILabel()
    navTitleLabel3.text = "Activity"
    navTitleLabel3.font = UIFont(name: "Helvetica", size: 20)
    navTitleLabel3.textColor = UIColor.whiteColor()

    var orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0)
    var gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0)


    ctr1.title = "Ctr1"
    ctr1.view.backgroundColor = UIColor.whiteColor()
    ctr2.title = "Ctr2"
    ctr2.view.backgroundColor = UIColor.yellowColor()
    ctr3.title = "Ctr3"
    ctr3.view.backgroundColor = UIColor.blueColor()

    controller = SLPagingViewSwift(items: [navTitleLabel1, navTitleLabel2, navTitleLabel3], controllers: [ctr1,ctr2,ctr3], showPageControl: false)

    controller?.currentPageControlColor = UIColor.whiteColor()
    controller?.tintPageControlColor = UIColor(white: 0.799, alpha: 1.0)
    controller?.pagingViewMoving = ({ subviews in
        for v in subviews {
            var lbl = v as UILabel
            var c = gray

            if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
                c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
            }
            else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
                c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
            }
            else if(lbl.frame.origin.x == 145){
                c = orange
            }
            lbl.textColor = c
        }
    })


    controller?.didChangedPage = ({ currentIndex in
        println(currentIndex)
    })

    self.setViewControllers([controller!], animated: true)
}

PersonTableViewController是自定义的TableViewController,在该控制器中,我使用了自定义的tableviewcell。

以下是PersonTableViewController的代码:

    override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PersonCell", forIndexPath: indexPath) as PersonTableViewCell
    cell.name.text = "aaa"

    return cell
}

1 个答案:

答案 0 :(得分:0)

您的viewDidLoad方法应该使用表视图注册单元类。 UITableViewController的方法是:

func registerClass(_ cellClass: AnyClass,forCellReuseIdentifier identifier: String)

所以,在你的情况下,它将是:

ctr1.registerClass(PersonTableViewCell.self, forCellReuseIdentifier: "PersonCell")