2016-05-20 83 views
0

我刚刚开始使用Swift,并且遇到了一些有关自定义UITableViewCell的奇怪问题。问题是我需要在每个表单元格中有一个白色背景,如hereSwift - 自定义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是链接到故事板视图。我已经使用故事板视图中的标识符字段来链接单元格。

+0

你可以在IB中显示你的细胞吗? –

+0

@AlexKosyakov [Here](http://i.imgur.com/LhgvV7h.jpg)是我的故事板的屏幕截图。这有帮助吗? – sgilroy09

+0

我看不到,您是否对您的标签和按钮的超级视图设置了约束? –

回答

0

您有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. 
} 


} 
+0

是的,我没有使用任何委托功能,所以我不需要它(但)。然而,我陷入了一些混乱,我发现故事板中有一些古怪的隐含高度限制。 – sgilroy09

相关问题