2016-05-20 3 views
0

Я только начинаю с Swift, и у меня возникают некоторые странные проблемы с пользовательским UITableViewCell. Проблема в том, что мне нужно иметь белый фон внутри каждой ячейки таблицы, как видно here.Swift - UIView внутри пользовательского 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 стилизована в соответствии с раскадрой. Когда я пытаюсь изменить фон метки или кнопку с помощью раскадровки, ничего не меняется. Никакой ярлык и просто общая кнопка. Странная часть - я могу установить изображения в UIImageView через класс ViewController.

Любые мысли?

EDIT: Here - ссылка на раскадровку. Я использовал поле идентификатора в представлении раскадровки, чтобы связать ячейку.

+0

Можете ли вы показать свою камеру в IB? –

+0

@AlexKosyakov [Здесь] (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

Смежные вопросы