我试图让UIImage出现在TableView中。我创建了一个结构,请参见下文:
struct City {
let name: String
let coordinates: (lat: Double, lon: Double)
let image: UIImage
}
在我的MasterTableViewController中,我创建了以下数组。
override func viewDidLoad() {
super.viewDidLoad()
cities = []
let london = City(name: "Melbourne", coordinates: (-37.8436, 144.9510), image:UIImage(named: "Australia.png")!)
let melbourne = City(name: "Sepang", coordinates: (2.7623, 101.7386), image:UIImage(named: "Malaysia.png")!)
let china = City(name: "Shanghai", coordinates: (31.2304,121.4737), image:UIImage(named: "China.png")!)
let saopaulo = City(name: "Bahrain", coordinates: (26.0316,50.5129), image:UIImage(named: "Qatar.png")!)
let nurbergring = City(name: "Barcelona", coordinates: (41.5666,2.2642), image:UIImage(named: "Spain.png")!)
let monaco = City(name: "Monaco", coordinates: (-35.0617, 138.6155), image:UIImage(named: "France.png")!)
let canada = City(name: "Canada", coordinates: (45.5066, -73.5242), image:UIImage(named: "Canada.png")!)
let austria = City(name: "Austria", coordinates: (47.2234, 14.7606), image:UIImage(named: "Austria.png")!)
let british = City(name: "Silverstone", coordinates: (52.0753, -1.0155), image:UIImage(named: "United Kingdom.png")!)
let hungary = City(name: "Hungaroring", coordinates: (47.5829, 19.2486), image:UIImage(named: "HUngary.png")!)
let belgium = City(name: "Belgium", coordinates: (50.4370, 5.9680), image:UIImage(named: "Belgium.png")!)
let italy = City(name: "Monza", coordinates: (45.6140, 9.2823), image:UIImage(named: "Italy.png")!)
let singapore = City(name: "Singapore", coordinates: (1.2842, 103.8583), image:UIImage(named: "Singapore.png")!)
let japan = City(name: "Suzuka", coordinates: (34.8410, 136.5550), image:UIImage(named: "Japan.png")!)
let russian = City(name: "Sochi", coordinates: (43.6028, 39.7341), image:UIImage(named: "Russia.png")!)
let usa = City(name: "Circuit of the Americas", coordinates: (30.1384, -97.6352), image:UIImage(named: "United States.png")!)
let mexico = City(name: "Mexico", coordinates: (19.4326, -99.1332), image:UIImage(named: "Mexico.png")!)
let brazil = City(name: "Brazil", coordinates: (-23.5505, -46.6333), image:UIImage(named: "Brazil.png")!)
let abudhabi = City(name: "Yas Marina Circuit", coordinates: (24.4729, 54.6063), image:UIImage(named: "Qatar.png")!)
我不确定我是否应该在故事板中为单元格添加UIImage,然后为png文件创建一个单独的数组?或者我应该刚添加UIImage。 此外,图像是标志图标,因为我已经做了44 * 44像素。这是在tableView Cell中添加图像的正确规范吗?
答案 0 :(得分:0)
目前尚不清楚在哪里使用ViewDidLoad函数中创建的城市......
无论如何,我认为最好的方法是拥有一系列城市,你可以将所有这些城市都附加到。然后在你的cellForRowAtIndexPath中使用该数组。
MasterTableViewController的一些基本示例代码可以帮助您实现
class MasterTableViewController: UITableViewController
@IBOutlet var peopleTable: UITableView! // make sure you connect the outlet from your Storyboard
var cities = [city]()
func loadCities () {
cities.append (City(name: "Melbourne", coordinates: (-37.8436, 144.9510), image:UIImage(named: "Australia.png")!))
cities.append (City(name: "Sepang", coordinates: (2.7623, 101.7386), image:UIImage(named: "Malaysia.png")!))
//... for all the cities
}
override func viewDidLoad() {
super.viewDidLoad()
loadCities ()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell (style:UITableViewCellStyle.Default, reuseIdentifier: "myCell") as! myCustomCell
cell.myTextLabel = cities[indexPath.row].name
cell.myImageView = cities[indexPath.row].image
//... other cell properties
}
确保你的故事板中定义了myTextLabel,myImageView并连接到myCustomCell中的Outlets