![](/img/trans.png)
[英]Swift UIView custom view won't update after ViewController action
[英]Swift - UIView inside custom UITableViewCell won't update buttons/labels
我剛剛開始使用Swift,並且自定義UITableViewCell出現了一些奇怪的問題。 問題是我需要有每個表單元格中的白色背景,看到這里 。
我創建了一個自定義UITableViewCell類,並為圖像,標簽和按鈕創建了IBOutlets,如下所示:
import UIKit
class LocationTableViewCell: UITableViewCell {
@IBOutlet var locationImageView: UIImageView!
@IBOutlet var locationButton: UIButton!
@IBOutlet var locationLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
我的視圖控制器代碼是:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet var locationTableView: UITableView!
var skiLocation = Location(name: "Banff", picture: UIImage(named: "ski.jpeg")!)
var tundraLocation = Location(name: "Yellowknife", picture: UIImage(named: "tundra.jpeg")!)
var beachLocation = Location(name: "Cancun", picture: UIImage(named: "beach.jpeg")!)
var locations: [Location] = []
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locations.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: LocationTableViewCell = tableView.dequeueReusableCellWithIdentifier("location", forIndexPath: indexPath) as! LocationTableViewCell
cell.locationLabel.text = self.locations[indexPath.row].name
cell.locationImageView.image = self.locations[indexPath.row].picture
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locations = [skiLocation, tundraLocation, beachLocation]
self.locationTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我通過在每個表視圖中創建一個UIView並將按鈕,標簽和圖像放置在UIView中來解決此問題。 這只是為了獲得白色背景。 但是,當我運行代碼時,所有內容均按預期顯示,除了沒有UILabel且UIButton根據情節提要樣式化。 當我嘗試通過故事板更改標簽的背景或按鈕時,沒有任何變化。 沒有標簽,只有通用按鈕。 奇怪的是,我能夠通過ViewController類將圖像設置為UIImageView。
有什么想法嗎?
編輯: 這是指向情節提要視圖的鏈接。 我在情節提要視圖中使用了標識符字段來鏈接單元格。
您有UITableViewDataSource
但缺少UITableViewDelegate
。
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var locationTableView: UITableView!
var skiLocation = Location(name: "Banff", picture: UIImage(named: "ski.jpeg")!)
var tundraLocation = Location(name: "Yellowknife", picture: UIImage(named: "tundra.jpeg")!)
var beachLocation = Location(name: "Cancun", picture: UIImage(named: "beach.jpeg")!)
var locations: [Location] = []
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locations.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: LocationTableViewCell = tableView.dequeueReusableCellWithIdentifier("location", forIndexPath: indexPath) as! LocationTableViewCell
cell.locationLabel.text = self.locations[indexPath.row].name
cell.locationImageView.image = self.locations[indexPath.row].picture
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locations = [skiLocation, tundraLocation, beachLocation]
self.locationTableView.dataSource = self
self.locationTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.