我有一些基本代码,尝试加载带有一些单元格的UITableView。我在Storyboard中设置了单元格ID,并为名为" DemoListCell"的单元格创建了一个swift文件和xib。虽然我不会在我的项目中使用故事板,但我仍然会收到此错误。 有任何想法吗?谢谢你们所有人。
import UIKit
// MARK:
class ViewController: UIViewController,UITableViewDataSource {
let cellId = "DemoListID"
let TopTableView = UITableView()
let PageTableView = UITableView()
let PageScrollView = UIScrollView()
var CellsIdentifier:String = ""
var NumbersofTab:CGFloat = 4
var Number:CGFloat = 4
var Size:CGFloat = 0
var OldPoint:CGFloat = 0
var NewPoint:CGFloat = 0{
didSet{
TopTableView.reloadData()
}
}
// MARK: - ViewDidLoad and init()
override func viewDidLoad() {
let TopWidth = self.view.frame.size.width
Size = TopWidth
let TopHeight:CGFloat = 50
let TopY = 0 - (TopWidth - TopHeight)/2
let TopX = 0 + (TopWidth - TopHeight)/2
let PageY = TopHeight
let PageHeight = self.view.frame.size.height - TopHeight
//TopTableView
TopTableView.frame = CGRectMake(TopX, TopY, TopHeight,TopWidth)
TopTableView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI/2))
TopTableView.showsVerticalScrollIndicator = false
TopTableView.backgroundColor = UIColor.yellowColor()
TopTableView.dataSource = self
TopTableView.delegate = self
//PageTableView
PageTableView.frame = CGRectMake(0, 0, TopWidth,PageHeight)
let nib = UINib(nibName: "DemoListCell", bundle: nil) //Cell's Name
self.PageTableView.registerNib(nib, forCellReuseIdentifier: cellId)
PageTableView.backgroundColor = UIColor.blueColor()
//PageScrollView
PageScrollView.frame = CGRectMake(0, PageY, TopWidth,PageHeight)
PageScrollView.delegate = self
PageScrollView.contentSize = CGSize(width: TopWidth * NumbersofTab, height: PageScrollView.frame.height)
PageScrollView.pagingEnabled = true
PageScrollView.alwaysBounceHorizontal = false
PageScrollView.alwaysBounceVertical = false
//addSubview
self.view.addSubview(TopTableView)
PageScrollView.addSubview(PageTableView)
self.view.addSubview(PageScrollView)
}
}
// MARK: - UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate{
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
OldPoint = PageScrollView.contentOffset.x
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
NewPoint = PageScrollView.contentOffset.x
print("\( NewPoint)")
}
} // MARK: - UITableViewDalegate
extension ViewController:UITableViewDelegate{
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellId, forIndexPath: indexPath) as! DemoListCell
if tableView.isEqual(TopTableView){
var cell:TopTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cd") as? TopTableViewCell
if (cell == nil) {
cell = TopTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cd")
switch indexPath.row{
case 0:
cell.backgroundColor = UIColor.greenColor()
case 1:
cell.backgroundColor = UIColor.redColor()
case 2:
cell.backgroundColor = UIColor.yellowColor()
case 3:
cell.backgroundColor = UIColor.whiteColor()
default:
cell.backgroundColor = UIColor.grayColor()
}
}
cell.TabTitle.text = "\(Int(NewPoint / Size))"
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let RowNumber:Int = 1
switch RowNumber{
default: return Int(NumbersofTab)
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let NumberofPages = Size / NumbersofTab
return NumberofPages
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIView.animateWithDuration(0.3) { () -> Void in
let Pages = self.Size * CGFloat(indexPath.row)
self.PageScrollView.contentOffset = CGPoint(x: Pages, y: 0)
self.TopTableView.reloadData()
self.NewPoint = self.PageScrollView.contentOffset.x
}
}
}
这是" CellId"细胞代码:
class DemoListCell: UITableViewCell {
@IBOutlet weak var cellImg: UIImageView!
@IBOutlet weak var cellLabel: UILabel!
@IBOutlet weak var cellIcon: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
self.cellImg.layer.masksToBounds = true
self.cellImg.layer.cornerRadius = 20.0
self.cellImg.layer.borderWidth = 0.0
self.cellImg.image = UIImage(named: "avatar.jpg")// Initialization code
self.cellIcon.image = UIImage(named: "Next")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
并且有" Cd"细胞代码:
class TopTableViewCell: UITableViewCell {
var TabTitle = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
TabTitle = UILabel(frame: CGRectZero)
TabTitle.textAlignment = NSTextAlignment.Right
TabTitle.numberOfLines = 1
TabTitle.backgroundColor = UIColor.clearColor()
TabTitle.textColor = UIColor(red: 130/255, green: 137/255, blue: 141/255, alpha: 1.0)
self.contentView.addSubview(self.TabTitle)
// 130 137 141
self.separatorInset = UIEdgeInsets(top: 0, left: 100, bottom: 0, right: -100)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
let marginX = CGFloat(0.0)
let marginY = CGFloat(0.0)
let width = CGFloat(30)
let height = self.contentView.frame.size.width
self.TabTitle.transform = CGAffineTransformMakeRotation(CGFloat(M_PI/2))
self.TabTitle.frame = CGRectMake(marginX, marginY, width, height)
}
}
答案 0 :(得分:0)
使用registerNib
注册nib时,会传入重用标识符(第二个参数)。该标识符需要在cellForRowAtIndexPath
中使用。但是,您的cellForRowAtIndexPath
使用"cd"
作为重用标识符,而不是cellId
,即您注册了nib的标识符。要解决此问题,请更改cellForRowAtIndexPath
中的两个重用标识符字符串:
var cell:TopTableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellId) as? TopTableViewCell
if (cell == nil) {
cell = TopTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
修改强>
我的错误,我没有看到cellForRowAtIndexPath
顶部的另一个出队。但是,我只看到viewDidLoad
中注册了一个笔尖,这意味着TopTableViewCell
从未使用标识符"cd"
注册。