2014-09-21 151 views
0

为了能够动态调整单元格的内容,我在表格单元格中创建了两个自定义标签。我第一次尝试使用“小标题”风格,除了单元格没有调整我想要的方式,它看起来非常混乱。访问自定义表格单元格标签

我的问题是:如何访问这些标签以将我的API从我的API附加到它们?

查看控制器代码:

import UIKit 

class nyheterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol { 

    @IBOutlet weak var nyheterTableView: UITableView! 
    @IBOutlet weak var titleLabel: UILabel! 

    var searchResultsData: NSArray = [] 
    var api: APIController = APIController() 

    func JSONAPIResults(results: NSArray) { 
     dispatch_async(dispatch_get_main_queue(), { 
      self.searchResultsData = results 
      print(self.searchResultsData) 
      self.nyheterTableView.reloadData() 
     }) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var APIBaseUrl: String = "http://*.se/*/*.php" 
     var urlString:String = "\(APIBaseUrl)" 

     //Call the API by using the delegate and passing the API url 
     self.api.delegate = self 
     api.GetAPIResultsAsync(urlString, elementName:"news") 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     //print(self.searchResultsData.count) 
     return self.searchResultsData.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cellIdentifier: String = "nyheterResultsCell" 


     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell 

     //nyheterTableViewCell.cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier); 

     //Create a variable that will contain the result data array item for each row 
     var cellData: NSDictionary = self.searchResultsData[indexPath.row] as NSDictionary 
     //Assign and display the Title field 
     var releaseDate: String = cellData["date"] as String 
     var titleVar: String = cellData["title"] as String 
     var titleMix: String = "\(titleVar)" + " - " + "\(releaseDate)" 

     cell.textLabel?.text = titleMix //textLabel worked out fine using "Subtitle" style. 

     // Get the release date string for display in the subtitle 

     cell.detailTextLabel?.text = cellData["content"] as String? //Same 

     return cell 
    } 
} 

我明白,我不能没有以某种方式将它们连接到ViewController访问这些标签。创建网点到ViewController会产生一个错误,我无法使用从原型单元到ViewController的连接。 因此,我创建了一个新课程,名为nyheterTableViewCell,我将其连接到表格单元格并将出口连接到我的标签。

nyhterTableViewCell代码:

import UIKit 

class nyheterTableViewCell: UITableViewCell { 

    @IBOutlet weak var nyhetLabel: UILabel! 
    @IBOutlet weak var titleLabel: 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 
    } 

} 

我在斯威夫特的编程和Xcode的一个初学者。

干杯!

回答

2

您不需要连接到视图控制器的标签。你自定义表格视图单元格的例子看起来是正确的。

要访问标签属性,你会想行

let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

改变

let cell: nyheterTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as nyheterTableViewCell