自定义UITableViewCell中的EXC_BAD_ACCESS

时间:2015-10-11 19:18:02

标签: ios swift uitableview

在过去的一天左右,我一直在撞墙,试图弄清楚这个问题,所以我希望有人可以提供帮助!

我只是想创建一个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时,它就会爆炸:(

编辑:

这就是我的堆栈的样子。我非常确定我在 GfyTableViewCell.swift 中的覆盖init()函数中做错了什么,但我不知道那是什么:

call stack

3 个答案:

答案 0 :(得分:4)

这里的问题是init方法调用自身。替换以下行:

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

with:

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

如果你自己调用一个方法,它将递归调用自己,直到程序最终因堆栈溢出或内存不足而崩溃。这与EXC_BAD_ACCESS崩溃的原因并不明显,但这可能会导致一个实例无法实际分配。

答案 1 :(得分:0)

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

而不是打电话:

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

似乎我需要放弃convenience并打电话:

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

然后我就可以像Anthony上面发布的那样,在没有任何错误的情况下致电super.init(style: style, reuseIdentifier: reuseIdentifier)

答案 2 :(得分:0)

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

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