2014-09-21 3 views
0

표 셀에 두 개의 사용자 정의 레이블을 작성하여 셀 내용의 크기를 동적으로 조정할 수 있습니다. 먼저 "자막"스타일을 사용해 보았는데이 셀은 내가 원하는대로 크기를 조정하지 못했고 정말 엉망이었습니다.사용자 정의 표 셀 레이블에 액세스

제 질문은 : 내 API의 값을 API에 추가하려면 어떻게해야합니까?

보기 컨트롤러 코드 : 나는 어떻게 든 ViewController에 연결하지 않고이 라벨에 액세스 할 수없는 것을 이해

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으로 사용할 수 없다는 오류가 발생합니다. 그래서 테이블 셀에 연결하고 레이블에 콘센트를 연결 한 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 
    } 

} 

나는 스위프트 - 프로그래밍 및 엑스 코드에서 초보자입니다.

건배!

답변

2

보기 컨트롤러에 연결된 레이블이 필요하지 않습니다. 사용자 정의 테이블보기 셀의 예제가 올바르게 보입니다.

레이블 속성에 액세스하려면, 당신은에 선

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

을 변경하려는거야

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

관련 문제