我刚刚开始使用Swift,并且遇到了一些有关自定义UITableViewCell的奇怪问题。问题是我需要在每个表单元格中有一个白色背景,如here。Swift - 自定义UITableViewCell内的UIView不会更新按钮/标签
我已经创建了一个图像,标签和按钮自定义的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。
有什么想法?
编辑:Here是链接到故事板视图。我已经使用故事板视图中的标识符字段来链接单元格。
你可以在IB中显示你的细胞吗? –
@AlexKosyakov [Here](http://i.imgur.com/LhgvV7h.jpg)是我的故事板的屏幕截图。这有帮助吗? – sgilroy09
我看不到,您是否对您的标签和按钮的超级视图设置了约束? –