简体   繁体   中英

Custom UITableView Cell is Causing An Error

My app is a simple RSS Reader that uses a tableview to display the list of articles. In order to display a picture from each of the articles in a tableview cell as well as the title and author of the articles, I used a custom UITableViewCell . I made a class for the tableview cell and created outlets for the imageview and labels from a prototype cell in the storyboard to the class. The code for my custom tableview cell is as follows:

import UIKit

class itemTableViewCell: UITableViewCell {
@IBOutlet weak var itemTitleLabel: UILabel!


@IBOutlet weak var itemImageView: UIImageView!
@IBOutlet weak var itemAuthorLabel: 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
    }
}

Furthermore, in my UITableViewController , I have the following code for the cellForRowAtIndexPath method:

override func tableView(tableView: UITableView, cellForRowAtIndexPath 
indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("cell", 
forIndexPath: indexPath) as! itemTableViewCell

   cell.itemImageView.image = UIImage(named: "placeholder")
    cell.backgroundColor = UIColor.clearColor()

    let item = feedItems[indexPath.row] as MWFeedItem
    cell.itemAuthorLabel.text = item.author
    cell.itemTitleLabel.text = item.title




    if item.content != nil {

        let htmlContent = item.content as NSString
        var imageSource = ""

        let rangeOfString = NSMakeRange(0, htmlContent.length)
        let regex = try? NSRegularExpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [])

        if htmlContent.length > 0 {
            let match = regex?.firstMatchInString(htmlContent as String, options: [], range: rangeOfString)

            if match != nil {
                let imageURL = htmlContent.substringWithRange(match!.rangeAtIndex(2)) as NSString
                print(imageURL)

                if NSString(string: imageURL.lowercaseString).rangeOfString("feedburner").location == NSNotFound {
                    imageSource = imageURL as String

                }

            }
        }

        if imageSource != "" {
            cell.itemImageView.setImageWithURL(NSURL(string:                       
imageSource)!, placeholderImage: UIImage(named: "placeholder"))
        }
        else{
            cell.itemImageView.image = UIImage(named: "placeholder")
        }

    }
    return cell
}

I also added

    var nibName=UINib(nibName: "itemTableViewCell", bundle:nil)
    tableView.registerNib(nibName, forCellReuseIdentifier: "cell")
    tableView.registerClass(itemTableViewCell.self,     
    forCellReuseIdentifier: "cell")

into my viewDidLoad() method.

Whenever I run the app, I get the classic "unexpectedly found nil while unwrapping an Optional value" error at the

cell.itemImageView.image = UIImage(named: "placeholder")

in my cellForRowAtIndexPath method.

Thank you so much for any help!

Remove the line

tableView.registerClass(itemTableViewCell.self, forCellReuseIdentifier: "cell")

because it's replacing the previous line with a request to instantiate the class without the nib so you don't have anything that's defined in the nib...


Based on the comments below both of the code lines should be removed as they're replacing any registration that the storyboard should make. The storyboard cell needs to have the cell identifier added so that it's registered during the storyboard instantiation and can then be used to dequeue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM