我用swift自定義了一個cell,同時復寫了init方法,但是每回調(diào)用的時候,都不走
if cell.isEqual(nil) {
cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: "cell", tag:indexPath.row)
}
原因是每回在dequeueReusableCell的時候,就莫名的調(diào)用了
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
而我真正其實是希望進入自己復寫的init方法,如下
init(style: UITableViewCellStyle, reuseIdentifier: String?, tag: Int) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = UIColor.white
upInfoCell()
}
我的完整代碼:
tableViewCell
class WYLClockDetailCell: UITableViewCell {
var cellTitle: UILabel?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//每次都進入這個
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
init(style: UITableViewCellStyle, reuseIdentifier: String?, tag: Int) {
//而我希望進入這個方法
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = UIColor.white
upInfoCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
tableview的創(chuàng)建方法:
override func viewDidLoad() {
super.viewDidLoad()
dataArray = ["內(nèi)容","日期","時間","是否提前","重復","鈴聲","細節(jié)"]
let tableView:UITableView = UITableView.init()
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = UIColor.init(red: CGFloat.init(arc4random()%255)/255.0, green: CGFloat.init(arc4random()%255)/255.0, blue: CGFloat.init(arc4random()%255)/255.0, alpha: 1)
tableView.bounces = false
tableView.separatorStyle = .none;
self.view.addSubview(tableView)
tableView.register(WYLClockDetailCell.self, forCellReuseIdentifier: "cell")
weak var weakSelf = self
tableView.snp.makeConstraints { (maker) in
maker.bottom.equalTo(weakSelf!.view).offset(-kRealValue()*5.5)
maker.left.equalTo(weakSelf!.view).offset(0)
maker.right.equalTo(weakSelf!.view).offset(0)
maker.height.equalTo(46*dataArray!.count)
}
self.view.backgroundColor = UIColor.init(red: CGFloat.init(arc4random()%255)/255.0, green: CGFloat.init(arc4random()%255)/255.0, blue: CGFloat.init(arc4random()%255)/255.0, alpha: 1)
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:WYLClockDetailCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WYLClockDetailCell
if cell.isEqual(nil) {
cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: "cell", tag:indexPath.row)
}
return cell
}
還有一個問題是整個tableview都無法復用了,每次都會直接從dequeueReusableCell直接生成一個cell,請問哪里出了問題,怎么解決呢
去掉register,因為register會直接init一個cell出來
tableView.register(WYLClockDetailCell.self, forCellReuseIdentifier: "cell")
要用 ? 而不是 ! 因為!會直接強制出一個cell
var cell = tableView.dequeueReusableCell(withIdentifier:idf) as? WYLClockDetailCell
if cell == nil {
cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: idf, tag: indexPath.row)
}
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務機構(gòu),發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。