2017-03-12 10 views
0

enter image description herehttps://i.stack.imgur.com/pRZu5.png 안녕하세요, 저는 이름과 특정 개체의 pointsWorth를 tableview에 표시하려고합니다. 하지만 xcode에 대한 missionTitleLabel은 missionCell에서 사용할 수 없습니다. 내가 만든 객체의 정보를 표시 할 수 있습니까?indexpath에있는 행에 대해 셀에 데이터 모델 내용을 표시하는 방법은 무엇입니까?

내가 얻을 수있는 도움에 감사드립니다! MasterViewController.swift :

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) 
    let event = self.fetchedResultsController.object(at: indexPath) 
    self.configureCell(cell, withEvent: event) 

    let mission = missions[indexPath.row] 
    MissionCell.missionTitleLabel?.text = mission 

    return cell 
} 

예 데이터 모델 : 여기

내 코드

열거 CategoryEnum { A 경우 경우 B 경우 C 케이스 D } 공용 클래스 미션 {

var name: String 
var pointsWorth: Int 
var colorTheme: UIColor 
var description: String 
var category: CategoryEnum 

init(name: String, pointsWorth: Int, colorTheme: UIColor, description: String, category: CategoryEnum) { 
    self.name = name 
    self.pointsWorth = pointsWorth 
    self.colorTheme = colorTheme 
    self.description = description 
    self.category = category 
} 
} let mission1 = Mission (이름 : "a", pointsWorth : 50, colorTheme : .blue, 설명 : "a는 알파벳 주먹 글자"카테고리 : .a)

let mission2 = Mission 이름 : "b", pointsWorth : 60, colorTheme : .red, description : "b는 알파벳의 두 번째 글자"카테고리 : .b)

var 임무 : [임무 1, 임무 1, 임무 2]

MissionCell.swift :

import UIKit 

클래스 MissionCell :있는 UITableViewCell {

@IBOutlet weak var missionTitleLabel: UILabel! 
@IBOutlet weak var missionPointLabel: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

} 

}

+0

'임무'의 유형은 무엇입니까? 그것은 끈인가? –

+0

예 문자열입니다 – Tromben

답변

0

여기 MissionCellcell 변수를 사용해야합니다.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as! MissionCell 
    let event = self.fetchedResultsController.object(at: indexPath) 
    self.configureCell(cell, withEvent: event) 

    let mission = missions[indexPath.row] 
    cell.missionTitleLabel?.text = mission.name 

    return cell 
} 

희망이 있습니다.

+0

감사! 코드에서 오류가 제거되었지만 앱을 실행할 때 이름이 tableview에 표시되지 않습니다. – Tromben

+0

'당신은 함수를 재정의 할 수 있습니다 tableView (_ tableView : UITableView, cellForRowAt indexPath : IndexPath) -> UITableViewCell'? 코드가 여기에서 트리거 되었습니까? –

+0

코드가 여기에서 트리거되지 않는 것 같습니다. – Tromben

0

당신은 다음과 같은 값을 지정해야합니다 :

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as! MissionCell 
    let event = self.fetchedResultsController.object(at: indexPath) 
    self.configureCell(cell, withEvent: event) 

    let mission = missions[indexPath.row] 
    cell.missionTitleLabel?.text = mission.name 


    return cell 
} 

당신이 확실히 알고 MissionCell에 힘 캐스트를 사용하여 세포의 한 종류가 있다는에만 있음을 유의하십시오 그렇지 않으면 충돌합니다. 고려 :

if let cell = cell as? MissionCell { 
    cell.missionTitleLabel?.text = mission.name 

} 
+0

답변 해 주셔서 감사합니다! 이 작업을 수행 한 후 다음 메시지가 나타납니다. String 유형의 Mission 유형 값을 할당 할 수 없습니까? 개체 이름 속성에 액세스 할 수있는 방법이 있습니까? – Tromben

+0

'mission.name'을 사용해야합니다. – orxelm

+0

감사합니다 orxelm! 이 오류가 제거되었지만 응용 프로그램을 실행할 때 이름이 tableview에 표시되지 않습니다. – Tromben