2017-03-25 2 views
0

테이블에 레이블을 붙이려고했지만 이전에 보지 못한 오류가 발생했습니다. 다른 사람은 재산 오류 질문에 할당 할 수 없기 때문에 내 상황과 일치하지 않는 것 같아 여기에 질문을 게시하십시오. 스택 오버플로에 대해 배우고있는 모든 포인터 정말 감사하겠습니다. 감사!값에 할당 할 수 없음 : 함수 호출은 불변 값을 반환합니다.

import UIKit 

class SeventhViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return 15 

} 

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell 

    var newData = [String]() 

    newData = ["Impressions","Image Tapped","Description Tapped","Biography Tapped","Purchase Link Tapped","Added to Collection","Removed from Collection"] 

    //Programmatically create label 
    var impressionsLabel = UILabel(frame: CGRect(x: 280.0, y: 14.0, width: 300.0, height: 30.0)) 
    impressionsLabel.text = newData[indexPath.row] 
    impressionsLabel.tag = 1 
    impressionsLabel.font = UIFont(name: "Impressions", size: 17.0) 

    impressionsLabel.textColor = UIColor.darkGray 

    cell.addSubview(impressionsLabel) 
    **cell.contentView.viewWithTag(1) = newData[indexPath.row]** 
    impressionsLabel.tag = 1 

    return cell 

답변

0

오류는 라인이다 :

cell.contentView.viewWithTag(1) = newData[indexPath.row] 

두 문제. viewWithTag은 선택 사항 인 UIView을 반환합니다. 그런 다음 UIViewString을 할당하려고 시도합니다. 태그 1와보기를 가정

이 같은 것을 필요로하는 UILabel입니다 :이 안전하게 (1의 태그와 실제보기가없는 넣다) 가능성이 전무보기 액세스를 다루는

if let label = cell.contentView.viewWithTag(1) as? UILabel { 
    label.text = newData[indexPath.row] 
} 

및 해당보기를 UILabel으로 전송하려고 시도합니다 (보기가 실제로는 UILabel이 아님). 이러한 조건이 충족되어 실제로 레이블을 얻으면 레이블의 text 특성을 설정합니다.

앱과 관련이 없지만 (앱을 실행할 때 새로운 문제가 있음) 데이터 배열을 선언하고 설정 한 후 cellForRowAt 메소드에서 설정하는 것은 의미가 없으므로 특정 카운트를 다음과 같이 하드 코딩해야합니다. 귀하의 numberOfRowsInSection 방법.

newData을보기 컨트롤러의 속성으로 만들고 셀에 액세스 할 때마다 한 번 설정하는 것이 좋습니다. 그런 다음 카운트를 numberOfRowsInSection에 반환합니다.

+0

종합적인 답변을 보내 주셔서 감사합니다. 그것은 내 즉각적인 오류를 해결해주었습니다. 이제 'newData'속성을 살펴 보겠습니다. –

관련 문제