在Swift4上的TableViewCell上将图像设置为UIImageView时出错

时间:2018-04-12 13:11:14

标签: ios swift xcode uiimageview

我想将图像设置为UIImageView,我尝试了两种方法:在属性检查器上设置图像并以编程方式设置图像,但没有任何效果。 在复制到项目的Assets文件夹之前,我不知道图像是否需要预先设置大小。

的ViewController:

import UIKit

class ViewControllerListaObjetos: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableViewItens: UITableView!
var titlePage : String?
var listaDados : [[String:Any]] = []

override func viewDidLoad() {
    self.title = titlePage!
    self.tableViewItens.delegate = self
    self.tableViewItens.dataSource = self
    print("ViewDidLoad ViewControllerObjects")
    switch (titlePage!) {
    case "Produtos":
        print("Titulo -> Produtos")
        let lista = SQLite.readAll(tabela: Utils.dictDadoTabela[titlePage!]!, typeClass: Product.self)

        if lista.count > 0 {
            listaDados.append((lista.first?.attributes)!)
            lista.first?.toString()
        }
        break

    case "Vendas":
        print("Titulo -> Vendas")
        let lista = SQLite.readAll(tabela: Utils.dictDadoTabela[titlePage!]!, typeClass: SaleOrder.self)
        if lista.count > 0 {
            lista.first?.toString()
        }
        break

    case "Clientes":
        print("Titulo -> Clientes")
        let lista = SQLite.readAll(tabela: Utils.dictDadoTabela[titlePage!]!, typeClass: ResPartner.self)
        if lista.count > 0 {
            lista.first?.toString()
        }
        break

    case "Itens de Vendas":
        print("Titulo -> Itens de Vendas")
        let lista = SQLite.readAll(tabela: Utils.dictDadoTabela[titlePage!]!, typeClass: SaleOrderLine.self)
        if lista.count > 0 {
            lista.first?.toString()
        }
        break

    default:
        print("Nenhum tipo encontrado.")
        break
    }

    tableViewItens.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listaDados.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()

    switch (titlePage!) {

    case "Produtos":
        print("Title = Produtos")
        cell = getCellProduct(index: indexPath.row, tableView)!
        break

    case "Clientes":

        break

    default:
        print("Nao encontrou titulo da pagina certo!")
        break
    }

    return cell
}

// Carregar dados conforme tipo
func getCellProduct(index: Int, _ tableView: UITableView) -> TableViewCellProduct? {
    if let cell = self.tableViewItens.cellForRow(at: IndexPath(row: index, section: 1)) as? TableViewCellProduct {
        cell.imageProduct.image = UIImage(named: "produto.jpg")
        return cell
    }
    return nil
}

    func getCellResPartner() -> UITableViewCell {
        return UITableViewCell()
    }

}

我收到错误:

2018-04-12 10:45:36.295610-0300 Odoo_01[26861:981455] -[UIImageView _isResizable]: unrecognized selector sent to instance 0x7fab9b547d40
2018-04-12 10:45:36.305878-0300 Odoo_01[26861:981455] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView _isResizable]: unrecognized selector sent to instance 0x7fab9b547d40'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000113b1412b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000011033df41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000113b95024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKit                               0x0000000110e0df51 -[UIResponder doesNotRecognizeSelector:] + 295
    4   CoreFoundation                      0x0000000113a96f78 ___forwarding___ + 1432
    5   CoreFoundation                      0x0000000113a96958 _CF_forwarding_prep_0 + 120
    6   UIKit                               0x00000001115d55ad -[UIImageView(Pretiling) _updatePretiledImageCacheForImage:] + 78
    7   UIKit                               0x0000000110e0aed1 -[UIImageView _updateImageViewForOldImage:newImage:] + 369
    8   UIKit                               0x0000000110e05e86 -[UIImageView setImage:] + 390
    9   UIKit                               0x000000011101148c -[UITableViewCell setImage:] + 79
    10  Foundation                          0x000000010fd5ca63 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    11  UIKit                               0x0000000110cab288 -[UIView(CALayerDelegate) setValue:forKey:] + 172
    12  UIKit                               0x000000011106bc2d -[UIRuntimeOutletConnection connect] + 109
    13  CoreFoundation                      0x0000000113ab73cd -[NSArray makeObjectsPerformSelector:] + 317
    14  UIKit                               0x000000011106a5e3 -[UINib instantiateWithOwner:options:] + 1856
    15  UIKit                               0x0000000110d1f687 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 590
    16  Odoo_01                             0x000000010f5f7576 _T07Odoo_0126ViewControllerListaObjetosC14getCellProductAA05TablebgH0CSgSi5index_So07UITableB0CtF + 150
    17  Odoo_01                             0x000000010f5f6d34 _T07Odoo_0126ViewControllerListaObjetosC05tableB0So07UITableB4CellCSo0gB0C_10Foundation9IndexPathV12cellForRowAttF + 1780
    18  Odoo_01                             0x000000010f5f74bc _T07Odoo_0126ViewControllerListaObjetosC05tableB0So07UITableB4CellCSo0gB0C_10Foundation9IndexPathV12cellForRowAttFTo + 92
    19  UIKit                               0x0000000110d3b484 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 778
    20  UIKit                               0x0000000110d3ba2a -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    21  UIKit                               0x0000000110d011f6 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3031
    22  UIKit                               0x0000000110d232e6 -[UITableView layoutSubviews] + 176
    23  UIKit                               0x0000000110caba6d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439
    24  QuartzCore                          0x000000011a52461c -[CALayer layoutSublayers] + 159
    25  QuartzCore                          0x000000011a5287ad _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
    26  QuartzCore                          0x000000011a4af86c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 364
    27  QuartzCore                          0x000000011a4dc946 _ZN2CA11Transaction6commitEv + 500
    28  UIKit                               0x0000000110c04167 _afterCACommitHandler + 272
    29  CoreFoundation                      0x0000000113ab6c07 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    30  CoreFoundation                      0x0000000113ab6b5e __CFRunLoopDoObservers + 430
    31  CoreFoundation                      0x0000000113a9b124 __CFRunLoopRun + 1572
    32  CoreFoundation                      0x0000000113a9a889 CFRunLoopRunSpecific + 409
    33  GraphicsServices                    0x00000001190459c6 GSEventRunModal + 62
    34  UIKit                               0x0000000110bda5d6 UIApplicationMain + 159
    35  Odoo_01                             0x000000010f60d637 main + 55
    36  libdyld.dylib                       0x0000000115371d81 start + 1
    37  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我在Assests上加载图片。并在我的故事板中进行配置。

enter image description here

1 个答案:

答案 0 :(得分:0)

你需要获取单元格,在该方法中没有dequeueCell,使用self.tableView.cellForRow(at: desiredIndexPath)你可以得到你的单元格

func getCellProduct(index:Int,tableView:UITableView) ->TableViewCellProduct? {
  if let cell = self.tableView.cellForRow(at:  NSIndexPath(row: index, section: 0)) as? TableViewCellProduct {
     cell.imageProduct.image = UIImage(named:"producto")
     return cell
  } 
  return nil
}