2015-10-11 52 views
0

过去一天我一直在撞墙,或者试图弄清楚这个问题,所以我希望有人可以帮忙!EXC_BAD_ACCESS在自定义UITableViewCell

我只是想创建一个UITableViewCell的自定义子类,但我的应用程序不断崩溃在我的自定义TableViewCell的init函数中的EXC_BAD_ACCESS错误。我在Xcode的7.01

DiscoverViewController.swift

import UIKit 

class DiscoverViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    let networkInterface: GfyNetwork = GfyNetwork() 

    var gfyArray: Array<GfyModel> = [] 
    var tableView: UITableView = UITableView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.title = "Discover" 

     let navbar = self.navigationController!.navigationBar 
     navbar.tintColor = UIColor(red:0.32, green:0.28, blue:0.61, alpha:1.0) 

     networkInterface.getTrendingGfys("", completionHandler: printGfys) 

     tableView.frame   = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
     tableView.delegate  = self 
     tableView.dataSource = self 
     tableView.separatorStyle = .None 
     tableView.rowHeight  = 260 
     tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0) 
     tableView.registerClass(GfyTableViewCell.self, forCellReuseIdentifier: "gfycell") 

     self.view.addSubview(tableView) 
    } 

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

    func printGfys(gfyJSON: Array<GfyModel>) -> Array<GfyModel> { 
     // Array of fetched gfys 
     self.gfyArray = gfyJSON 
     // Update Tableview 
     dispatch_async(dispatch_get_main_queue()) { 
      self.tableView.reloadData() 
     } 
     return gfyJSON 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.gfyArray.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     guard let cell = tableView.dequeueReusableCellWithIdentifier("gfycell", forIndexPath: indexPath) as? GfyTableViewCell else { fatalError("unexpected cell dequeued from tableView") } 
     cell.gfy = self.gfyArray[indexPath.row] 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print("You selected cell #\(indexPath.row)!") 
    } 
} 

GfyTableViewCell.swift

import UIKit 

class GfyTableViewCell: UITableViewCell { 

    let padding: CGFloat = 5 

    var gfy: GfyModel! 

    var bgView: UIView! 
    var imageURL: UIImageView! 
    var title: UILabel! 

    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 
    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    convenience override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     self.init(style: style, reuseIdentifier: reuseIdentifier) // Error happens here 

     backgroundColor = UIColor.whiteColor() 
     selectionStyle = .None 

     bgView.frame = CGRectMake(8, 0, contentView.frame.width-16, 250) 
     bgView.layer.cornerRadius = 3 
     bgView.layer.borderColor = UIColor(red:0, green:0, blue:0, alpha:0.4).CGColor 
     bgView.layer.borderWidth = 0.5 
     bgView.clipsToBounds = true 
     bgView.backgroundColor = UIColor.whiteColor() 

     title.frame = CGRectMake(10, 210, bgView.frame.width-100, 10) 
     title.text = gfy.title 
     title.font = UIFont.systemFontOfSize(10) 

     imageURL.frame = CGRectMake(0, 0, bgView.frame.width, 200) 
     if let url = NSURL(string: gfy.thumbUrl) { 
      if let data = NSData(contentsOfURL: url){ 
       imageURL.contentMode = UIViewContentMode.ScaleAspectFill 
       imageURL.image = UIImage(data: data) 
      } 
     } 

     contentView.addSubview(bgView) 
     bgView.addSubview(imageURL) 
    } 

    override func prepareForReuse() { 
     super.prepareForReuse() 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 
    } 

} 

任何帮助将非常感激。使用标准UITableViewCells当应用程序的工作原理,但只要我尝试添加自定义tableviewcells,它炸毁:(

编辑:

这是我的筹码是什么样子我敢肯定我。在做错事在我重写init()函数GfyTableViewCell.swift,但我不知道那是什么:

call stack

回答

3

这里的问题是,在init方法调用本身更换以下。 line:

self.init(style: style, reuseIdentifier: reuseIdentifier) 

有:

super.init(style: style, reuseIdentifier: reuseIdentifier) 

如果你调用它自己内部的方法将递归调用自身直到程序崩溃,最终因为一个堆栈溢出或耗尽内存。这并不明显,为什么这与EXC_BAD_ACCESS崩溃,但这可能导致一个实例未能实际分配。

+0

我真的认为这是事实,但是当我将其更改为super.init()的Xcode给了我下面的编译器错误: 'GfyTableViewCell.swift:37:15:便利初始化为“GfyTableViewCell”必须委托(使用'self.init')而不是链接到超类初始化器(使用'super.init')' – Sang

+0

实际上,从头开始。问题是我重写了便利初始值设定项。只要调用'override init(...)'而不是'convenience override init(...)',就可以毫无问题地使用'super.init(...)'。谢谢!将这个答案标记为已接受。 – Sang

+1

太棒了。这就说得通了。我以为你也可能会收到警告。在Swift中,每个初始化器都可能是一个方便的或者是一个指定的初始化器。指定的初始化程序应调用超类的初始化程序(如果存在)并完全初始化一个对象。一个方便的初始化器应该调用'self'上的另一个初始化器。所以,正如你所说,除了改变这一行之外,你还需要更改该初始化程序的声明以表明你符合那个标准。 –

0

哇,正如我所料,这是我的一个简单的错误。

,不再拨打电话:

convenience override init(style: UITableViewCellStyle, reuseIdentifier: String?) { ... } 

好像我需要沟convenience,只是称:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { ... } 

然后,我可以做安东尼上面贴和呼叫super.init(style: style, reuseIdentifier: reuseIdentifier)没有任何错误。

0

修复:对于Xcode 7.1.1中的自定义TableviewCell。

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    let cellT = cell as! CustomTableViewCellName 
    //enter code here 
}