填充Tableview时发送到实例的无法识别的选择器

时间:2015-11-20 12:14:05

标签: ios swift uitableview

当我尝试在我的视图控制器中填充tableview时,我收到此错误: [UITableViewCellContentView _isResizable]:无法识别的选择器发送到实例

import UIKit
import CoreLocation


class FirstViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{


    @IBOutlet var citta: AutoCompleteTextField!

    @IBOutlet var tableView: UITableView!


    let locationManager = CLLocationManager()
    var locality : String = ""
    var Locali = [Locale(Name: "Prova1", Address: "Via Prova ,1", Citta: "Bologna",Photo: UIImage(named: "Fav")!),Locale(Name: "Prova1", Address: "Via Prova ,1", Citta: "Bologna",Photo: UIImage(named: "Fav")!),Locale(Name: "Prova1", Address: "Via Prova ,1", Citta: "Bologna",Photo: UIImage(named: "Fav")!),Locale(Name: "Prova1", Address: "Via Prova ,1", Citta: "Bologna",Photo: UIImage(named: "Fav")!)]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
        }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
}
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks,error) -> Void in
            if error != nil{
                print("Error : " + error!.localizedDescription)
                return
            }
            if placemarks!.count > 0{
                let pm = placemarks![0]
                self.displayLocationInfo(pm)
            }})
    }
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error:" + error.localizedDescription)
    }
    func displayLocationInfo(placemarks: CLPlacemark)
    {
        self.locationManager.stopUpdatingLocation()
        self.citta.text = placemarks.locality!

    }
    func tableView(tableView : UITableView, numberOfRowsInSection section : Int) -> Int{
        return Locali.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell{
        let myCellToReturn = self.tableView.dequeueReusableCellWithIdentifier("Local_Cell", forIndexPath: indexPath) as! Local_Cell
        myCellToReturn.Nome?.text = Locali[indexPath.row].Name
        myCellToReturn.Address?.text = Locali[indexPath.row].Address
        myCellToReturn.Imago = UIImageView(image: Locali[indexPath.row].Image)


        return myCellToReturn
    }
}

Locale是我的自定义对象,Local_Cell是我的自定义单元格,这是代码:

import UIKit

class Local_Cell: UITableViewCell {

    @IBOutlet var Nome: UILabel!

    @IBOutlet var Imago: UIImageView!

    @IBOutlet var Address: UILabel!

    @IBOutlet var Fav_Button: UIButton!

    @IBOutlet var Menu_Button: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }


}

我尝试了一切,有谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

问题在于这行代码:

myCellToReturn.Imago = UIImageView(image: Locali[indexPath.row].Image)

ImagoIBOutlet,那么您为什么要实例化新的UIImageView并将其分配给Imago

试试这个

myCellToReturn.Imago.image = Locali[indexPath.row].Image

答案 1 :(得分:0)

这是因为您在arraycellForRowAtIndexPath:

中使用的对象名称不匹配

您正在数组中的对象Photo中设置图片名称,但您在Image

中使用对象名称cellForRowAtIndexPath:

而且,正如@Usama所说,你正在初始化一个新的UIImageView。因此,此处无需创建新的UIImageView,只需将image分配给现有的myCellToReturn.Imago = UIImageView(image: Locali[indexPath.row].Image)

替换代码

myCellToReturn.Imago.image = UIImage(named: Locali[indexPath.row].Photo)

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body style="height: 100%; margin:0; background-color: #FFFF00">
<header>
    <H1>Header of page with long list of items</H1>
</header>
<section style="position: absolute; top: 80px; bottom: 5px; width:100%; background-color: red">
    <div style="background-color: #BBBBBB">
        <div style="overflow-y: auto;">
            <ol>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
                <li><h3>Item in the list</h3></li>
            </ol>
        </div>
    </div>
</section>
</body>
</html>