问题:
根据我的表格视图,我想使用单个视图控制器来加载数据,特别是文本和图像indexPath.row
。所有单元格都连接到一个视图控制器。我正在从数组中加载标签和图像。
当 tableview 加载它崩溃并且我在这一行得到一个 EXC_BAD_INSTRUCTION 错误headerTitle.text = cellContent[indexPath.row]
代码:
此控制器用于 tableview 和 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
}
}
图片: