-1

Problem:

I want to use a single viewcontroller to load data, specifically text and images, according to the indexPath.row of my tableview. All cells are segued to the one viewcontroller. I am loading the labels and images from arrays.

When the tableview loads it crashes and I get a EXC_BAD_INSTRUCTION error on this line headerTitle.text = cellContent[indexPath.row]

Code:

This controller is used for the tableview and viewcontroller

import UIKit
import MapKit
import CoreLocation

class CCArtController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {

@IBOutlet weak var headerTitle: UILabel!
@IBOutlet weak var centerImage: UIImageView!
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var detailLabel: UITextView!

let locationManager = CLLocationManager()

var cellContent = [/*0*/"Arroyo", /*1*/"Buddy Rogers", /*2*/"Chopsticks", /*3*/"Colorful Fountain", /*4*/"Diamond", /*5*/"Dinah Shore Bridge Mosaics", /*6*/"Fountain of Life", /*7*/"Fountains", /*8*/"Historic Photo Collection", /*9*/"Holistic 77-A", /*10*/"Hollywood Heroes", /*11*/"Indian Gathering", /*12*/"Indian Women", /*13*/"Mountain Cathedrals", /*14*/"Old Firehouse", /*15*/"Perfect Union", /*16*/"Pickford Lizards", /*17*/"Saguaro and Barrell Cactus", /*18*/"Swiss Cheese", /*19*/"The New Beginning", /*20*/"Thunderbolt", /*21*/"Tic Toc", /*22*/"Wheel of Time", /*23*/"Arte Vita Gallery", /*24*/"Coachella Valley Art Scene", /*25*/"Colin Fisher Studios", /*26*/"Gary's Creative Warehouse", /*27*/"Leslie Jean Porter Gallery", /*28*/"Rebecca Fine Art Gallery", /*29*/"Adobe House", /*30*/"El Gallito Restaurant", /*31*/"The Agnes Pelton Home", /*32*/"The Val Samuelson Home", /*33*/"Willard Price House", /*34*/"Cathedral City Dog Park", /*35*/"Century Park", /*36*/"Dennis Keat Soccer Park", /*37*/"Memorial Park", /*38*/"Ocotillo Park", /*39*/"Panorama Park", /*40*/"Patriot Park", /*41*/"Town Square Park", /*42*/"2nd Street Park"]

var cellDetail = [/*0*/"City Hall First Floor Lobby", /*1*/"Town Square - Civic Center", /*2*/"Da Val Dr at Ramon Rd", /*3*/"Cathedral City Branch Library", /*4*/"Patriot Park at Big League Dreams", /*5*/"Dinah Shore Bridge", /*6*/"Town Square - Civic Center", /*7*/"Across From Town Square", /*8*/"City Hall Offices", /*9*/"The Passage through City Hall", /*10*/"Town Square, Civic Center", /*11*/"City Hall Offices", /*12*/"Date Palm Dr South of Gerald Ford Dr", /*13*/"Date Palm Dr South of Vista Chino", /*14*/"Commercial Rd", /*15*/"E Palm Canyon Near Date Palm Dr", /*16*/"Pickfair Theatre", /*17*/"Cathedral Canyon Dr", /*18*/"Front Lawn of City Hall", /*19*/"Buddy Rogers Dr", /*20*/"Cathedral Canyon Dr at Dinah Shore", /*21*/"Dinah Shore Dr, East of Date Palm Dr", /*22*/"Ramon Rd at Date Palm Dr", /*23*/"Perez Rd Near Date Palm Dr", /*24*/"E Palm Canyon Dr", /*25*/"Perez Rd Near Date Palm Dr", /*26*/"Date Palm Dr at Perez Rd", /*27*/"Perez Rd Near Cathedral Canyon Dr", /*28*/"Perez Rd Near Date Palm Dr", /*29*/"San Jacinto Rd", /*30*/"Grove St Near E Palm Canyon Dr", /*31*/"Cathedral Canyon Drive at F Street", /*32*/"Grandview Ave", /*33*/"Cathedral Canyon Drive at F Street", /*34*/"Buddy Rogers Ave at Allen Ave", /*35*/"Da Val Dr at Century Park Dr", /*36*/"30th Ave at Santoro Dr", /*37*/"Officer David Vasquez Rd", /*38*/"Moreno Rd at Ocotillo Rd", /*39*/"Tachevah Dr at Avenida Maravilla", /*40*/"Dinah Shore Dr at Da Val Dr", /*41*/"Buddy Rogers Ave", /*42*/"Date Palm Dr at Buddy Rogers Ave"]

var cellImage = [/*0*/"arroyofront3.jpg", /*1*/"br1.jpg", /*2*/"chopsticksfront.jpg", /*3*/"colorfulfount.jpg", /*4*/"d1.jpg", /*5*/"DinahShore1b.jpg", /*6*/"f5.jpg", /*7*/"fount4.jpg", /*8*/"indianfront2.jpg", /*9*/"hol5a.jpg", /*10*/"h1.jpg", /*11*/"indiangatheringfront.jpg", /*12*/"IndianWomen2.jpg", /*13*/"cathedrals.jpg", /*14*/"fh1.jpg", /*15*/"pu10.jpg", /*16*/"lizard.jpg", /*17*/"cac2.jpg", /*18*/"swiss2.jpg", /*19*/"nb5.jpg", /*20*/"thunderfront.jpg", /*21*/"tictoc.jpg", /*22*/"wheelfront.jpg", /*23*/"artevitafront.jpg", /*24*/"coachella2.jpg", /*25*/"colinfisherfront.jpg", /*26*/"garyfront2.jpg", /*27*/"lesliefront.jpg", /*28*/"rebeccalogo2.jpg", /*29*/"adobefront.jpg", /*30*/"egfront.jpg", /*31*/"agnesfront.jpg", /*32*/"valfront.jpg", /*33*/"pricefront.jpg", /*34*/"dogparkfront.jpg", /*35*/"centuryfront.jpg", /*36*/"soccerparkfront.jpg", /*37*/"memorialfront.jpg", /*38*/"ocotillofront.jpg", /*39*/"panoramafront.jpg", /*40*/"patriotfront.jpg", /*41*/"townsquarefront.jpg", /*42*/"2ndfront.jpg"]

@IBAction func back(sender: AnyObject) {

    dismissViewControllerAnimated(true, completion: nil)

}

@IBOutlet weak var tableview: UITableView!

struct Objects {
    var cellName : String!
    var sectionObjects : [String]!
    var detailObjects : [String]!
    var imageObjects : [String]!
}

var objectsArray = [Objects]()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchData()

    tableview.layer.cornerRadius = 10.0

    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()

    let radius = CLLocationDistance(160934) // 5 miles in meters

    objectsArray =
        [Objects(cellName: "Art",
            sectionObjects: [/*0*/"Arroyo", /*1*/"Buddy Rogers", /*2*/"Chopsticks", /*3*/"Colorful Fountain", /*4*/"Diamond", /*5*/"Dinah Shore Bridge Mosaics", /*6*/"Fountain of Life", /*7*/"Fountains", /*8*/"Historic Photo Collection", /*9*/"Holistic 77-A", /*10*/"Hollywood Heroes", /*11*/"Indian Gathering", /*12*/"Indian Women", /*13*/"Mountain Cathedrals", /*14*/"Old Firehouse", /*15*/"Perfect Union", /*16*/"Pickford Lizards", /*17*/"Saguaro and Barrell Cactus", /*18*/"Swiss Cheese", /*19*/"The New Beginning", /*20*/"Thunderbolt", /*21*/"Tic Toc", /*22*/"Wheel of Time"],
            detailObjects: [/*0*/"City Hall First Floor Lobby", /*1*/"Town Square - Civic Center", /*2*/"Da Val Dr at Ramon Rd", /*3*/"Cathedral City Branch Library", /*4*/"Patriot Park at Big League Dreams", /*5*/"Dinah Shore Bridge", /*6*/"Town Square - Civic Center", /*7*/"Across From Town Square", /*8*/"City Hall Offices", /*9*/"The Passage through City Hall", /*10*/"Town Square, Civic Center", /*11*/"City Hall Offices", /*12*/"Date Palm Dr South of Gerald Ford Dr", /*13*/"Date Palm Dr South of Vista Chino", /*14*/"Commercial Rd", /*15*/"E Palm Canyon Near Date Palm Dr", /*16*/"Pickfair Theatre", /*17*/"Cathedral Canyon Dr", /*18*/"Front Lawn of City Hall", /*19*/"Buddy Rogers Dr", /*20*/"Cathedral Canyon Dr at Dinah Shore", /*21*/"Dinah Shore Dr, East of Date Palm Dr", /*22*/"Ramon Rd at Date Palm Dr"],
            imageObjects: [/*0*/"arroyofront3.jpg", /*1*/"br1.jpg", /*2*/"chopsticksfront.jpg", /*3*/"colorfulfount.jpg", /*4*/"d1.jpg", /*5*/"DinahShore1b.jpg", /*6*/"f5.jpg", /*7*/"rf4.jpg", /*8*/"indianfront2.jpg", /*9*/"hol5a.jpg", /*10*/"h1.jpg", /*11*/"indiangatheringfront.jpg", /*12*/"IndianWomen2.jpg", /*13*/"cathedrals.jpg", /*14*/"fh1.jpg", /*15*/"pu10.jpg", /*16*/"lizard.jpg", /*17*/"cac2.jpg", /*18*/"swiss2.jpg", /*19*/"nb5.jpg", /*20*/"thunderfront.jpg", /*21*/"tictoc.jpg", /*22*/"wheelfront.jpg"]),
         Objects(cellName: "Art Galleries",
            sectionObjects: [/*23*/"Arte Vita Gallery", /*24*/"Coachella Valley Art Scene", /*25*/"Colin Fisher Studios", /*26*/"Gary's Creative Warehouse", /*27*/"Leslie Jean Porter Gallery", /*28*/"Rebecca Fine Art Gallery"],
            detailObjects: [/*23*/"Perez Rd Near Date Palm Dr", /*24*/"E Palm Canyon Dr", /*25*/"Perez Rd Near Date Palm Dr", /*26*/"Date Palm Dr at Perez Rd", /*27*/"Perez Rd Near Cathedral Canyon Dr", /*28*/"Perez Rd Near Date Palm Dr"],
            imageObjects: [/*23*/"artevitafront.jpg", /*24*/"coachella2.jpg", /*25*/"colinfisherfront.jpg", /*26*/"garyfront2.jpg", /*27*/"lesliefront.jpg", /*28*/"rebeccalogo2.jpg"]),
         Objects(cellName: "Historical Sites",
            sectionObjects: [/*29*/"Adobe House", /*30*/"El Gallito Restaurant", /*31*/"The Agnes Pelton Home", /*32*/"The Val Samuelson Home", /*33*/"Willard Price House"],
            detailObjects: [/*29*/"San Jacinto Rd", /*30*/"Grove St Near E Palm Canyon Dr", /*31*/"Cathedral Canyon Drive at F Street", /*32*/"Grandview Ave", /*33*/"Cathedral Canyon Drive at F Street"],
            imageObjects: [/*29*/"adobefront.jpg", /*30*/"egfront.jpg", /*31*/"agnesfront.jpg", /*32*/"valfront.jpg", /*33*/"pricefront.jpg"]),
         Objects(cellName: "Parks",
            sectionObjects: [/*34*/"Cathedral City Dog Park", /*35*/"Century Park", /*36*/"Dennis Keat Soccer Park", /*37*/"Memorial Park", /*38*/"Ocotillo Park", /*39*/"Panorama Park", /*40*/"Patriot Park", /*41*/"Town Square Park", /*42*/"2nd Street Park"],
            detailObjects: [/*34*/"Buddy Rogers Ave at Allen Ave", /*35*/"Da Val Dr at Century Park Dr", /*36*/"30th Ave at Santoro Dr", /*37*/"Officer David Vasquez Rd", /*38*/"Moreno Rd at Ocotillo Rd", /*39*/"Tachevah Dr at Avenida Maravilla", /*40*/"Dinah Shore Dr at Da Val Dr", /*41*/"Buddy Rogers Ave", /*42*/"Date Palm Dr at Buddy Rogers Ave"],
            imageObjects: [/*34*/"dogparkfront.jpg", /*35*/"centuryfront.jpg", /*36*/"soccerparkfront.jpg", /*37*/"memorialfront.jpg", /*38*/"ocotillofront.jpg", /*39*/"panoramafront.jpg", /*40*/"patriotfront.jpg", /*41*/"townsquarefront.jpg", /*42*/"2ndfront.jpg"])]

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    animateTable()

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

}

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

func animateTable() {
    tableview.reloadData()

    let cells = tableview.visibleCells
    let tableHeight: CGFloat = tableview.bounds.size.height

    for i in cells {
        let cell: UITableViewCell = i as UITableViewCell
        cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
    }

    var index = 0

    for a in cells {
        let cell: UITableViewCell = a as UITableViewCell
        UIView.animateWithDuration(1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            cell.transform = CGAffineTransformMakeTranslation(0, 0);
            }, completion: nil)

        index += 1
    }
}

func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
    print("Monitoring failed for region with identifier: \(region!.identifier)")
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Location Manager failed with the following error: \(error)")
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return objectsArray.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int {
    return objectsArray[section].sectionObjects.count
    //43
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return objectsArray[section].cellName
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont (name: "Avenir-Heavy", size: 18)
}

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

    let cell: UITableViewCell = (tableView.dequeueReusableCellWithIdentifier("ArtCell") as UITableViewCell!)
    if (cell == cell) {

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "ArtCell")

        let imageName = UIImage(named: objectsArray[indexPath.section].imageObjects[indexPath.row])

        cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
        cell.detailTextLabel?.text = objectsArray[indexPath.section].detailObjects[indexPath.row]
        cell.imageView?.image = imageName
        cell.backgroundColor = .clearColor()
        tableview.backgroundColor = UIColor(white: 1, alpha: 0.5)

        headerTitle.text = cellContent[indexPath.row]
        detailLabel.text = cellDetail[indexPath.row]
        centerImage.image = imageName
        backgroundImage.image = imageName


        //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        //let cell2 = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

        //let art = cellContent[indexPath.row]
        //cell.textLabel?.text = cellContent[indexPath.row]
        //cell.detailTextLabel?.text = self.cellDetail[indexPath.row]
        //cell.imageView?.image = imageName
        //cell.backgroundColor = UIColor.clearColor()
        //cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)

        return cell
    }
    return cell

}

}

Images:

viewcontroller in storyboard

Hunter
  • 313
  • 5
  • 20

1 Answers1

1

You are saying:

 @IBOutlet weak var headerTitle: UILabel!

So headerTitle is not something in your cell; it's just some outlet property of your CCArtController view controller. And you have never connected anything in your interface to that outlet, so it is nil. So when you try to set its text, you crash.

It seems likely that you're imagining that every cell has its own headerTitle. In that case, what you need here is a UITableViewCell subclass, with these @IBOutlet declarations there, and the elements of the cell hooked to them. You will then be able to set them by way of the cell.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I don't know what "work" even means. Your code makes no sense to me. Why on earth would you set the text of an outlet label every time `cellForRowAtIndexPath` is called??? However, _that is not what you asked_. You asked why you're crashing, and I think I explained that. The way your code is written, there needs to be an actual UILabel at the other end of this outlet. Right now, there isn't one. – matt Jun 08 '16 at 19:40
  • I don't think you really understand my question. Thanks for your input anyways. – Hunter Jun 08 '16 at 19:51
  • If your question is faulty, edit it. You have not explained what you _think_ this `headerTitle` represents, but I have told you correctly what it _is_. I understand everything you actually said. I don't think you really understand my answer and my comment. – matt Jun 08 '16 at 19:52
  • I have linked headerTitle to a label in my viewcontroller, but not in my tableview. How do I make it so that what I described above works? – Hunter Jun 08 '16 at 19:54
  • If the label is in a cell of the table view, then there is a different label for every cell, yes? So this needs to be a property of a UITableViewCell subclass, not a UIViewController subclass. – matt Jun 08 '16 at 21:03