简体   繁体   中英

Showing images in a UITableView with prototype cell

I want to make a UITableView with all members of a guild of mine. 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. I don't know what kind of type I have to give the UIImageViews for it to work.

With this code it's showing an error, that UIImageView is an undeclared type.

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. 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. Here is the Test for the 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. 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.

Did you add import UIKit to your file?

To use any apps UI(User Interface) element you need to import UIKit in your controller.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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