简体   繁体   English

在带有原型单元的UITableView中显示图像

[英]Showing images in a UITableView with prototype cell

I want to make a UITableView with all members of a guild of mine. 我想用我的行会的所有成员制作一个UITableView I have it configured to show information like name, address, phone, etc. But I also want to show two pictures of each person: 1) one with their portrait and 2) another with their emblem. 我将其配置为显示姓名,地址,电话等信息。但是我还想显示每个人的两张照片:1)一幅带有他们的肖像,2)另一幅带有他们的标志。 I don't know what kind of type I have to give the UIImageViews for it to work. 我不知道我必须赋予UIImageViews哪种类型才能工作。

With this code it's showing an error, that UIImageView is an undeclared type. 这段代码显示了一个错误,即UIImageView是未声明的类型。

import Foundation

struct Person {
    var name: String?
    var profession: String?
    var addressBlockOne: String?
    var addressBlockTwo: String?
    var phoneNumberPrivate: String?
    var phoneNumberMobile: String?
    var phoneNumberWork: String?
    var mail: String?
    var birthday: String?
    var admission: String?
    var portraitView: UIImageView?
    var emblemView: UIImageView?
}

EDIT: I have now imported the UIKit and changed the UIImageView to UIImage. 编辑:我现在导入了UIKit并将UIImageView更改为UIImage。 It's all working without error. 一切正常,没有错误。 But When I want to put a Image in the UIImageView, it doesn't show it in the simulator. 但是,当我想将图像放入UIImageView时,它不会在模拟器中显示。 Here is the Test for the TableCell: 这是TableCell的测试:

import Foundation
import UIKit

class PersonService {

private let persons: [Person] = [
    Person( name: "xy",
            profession: "yx",
            addressBlockOne: "street xy",
            addressBlockTwo: "yx",
            phoneNumberPrivate: "xxxx",
            phoneNumberMobile: "yyyy",
            phoneNumberWork: "-",
            mail: "zz@gmail.com",
            birthday: "xy",
            admission: "yx",
            portraitView: UIImage(named: "xy"),
            emblemView: UIImage(named: "yx")
    )

EDIT 2: It's not a problem with the constrain. 编辑2:约束不是问题。 The problem is, that it now only shows a picture when I put one in the UIImageView over the Storyboard ans it shows for all members the same picture. 问题是,当我在Storyboard上的UIImageView中放一张图片时,它现在只显示一张图片,并且为所有成员显示同一张图片。

Did you add import UIKit to your file? 您是否将导入UIKit添加到文件中?

To use any apps UI(User Interface) element you need to import UIKit in your controller. 要使用任何应用程序UI(用户界面)元素,您需要在控制器中导入UIKit。

Reference : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/ 参考: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/

您需要将图像作为UIImage存储在Person结构中-与将名称存储在String中(而不是在UITextView或UILabel中)完全相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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