2016-06-15 3 views
3

Swift 프로그래밍에서 새로운 기능을 제공하므로 저를 강하게 판단하지 마십시오. 문제가있는 곳을 이해하려고 많은 시간을 보냈지만 얻을 수는 없습니다. 나는 스토리 보드에 부착 한 라벨 - 나는 myLabel을 내 사용자 지정있는 UITableViewCell 클래스에서UILabel이 UITableViewCell 사용자 정의 클래스로 업데이트되지 않았습니다. Swift

@IBOutlet weak var myTableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    myTableView.delegate = self 
    myTableView.dataSource = self 
} 



func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 10 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell 

    cell.createNewCell() 
    return cell 
} 

:

은 내가 UIViewController에

의 UIViewController의 내부 jQuery과가있다.

@IBOutlet weak var myLabel: UILabel! 
    func createNewCell(){ 
     myLabel.text = "1234567" 
    } 

하지만 cellForRowAtIndexPath에 다음 줄 cell.textLabel!.text = "12312"를 추가하면 내가 커스텀있는 UITableViewCell 클래스를 통해 업데이트하려고 할 때 내 세포 업데이트, 셀은 업데이트되지 않습니다.

+0

나는 당신의 질문을 편집했지만 아직도 무엇을 요구하고 있는지 분명하지 않습니다. 마지막 두 문장이 명확하지 않습니다 – Honey

답변

0

셀의 데이터를 업데이트하는 경우 tablewView를 다시로드해야합니다. 데이터 소스 배열이 업데이트되었고 tableview에서 업데이트 된 데이터를 채우고 싶다면 reloadtableview 만 사용하십시오.

@IBOutlet weak var myTableView: UITableView! 
    var dataSource= [String]() 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    myTableView.delegate = self 
    myTableView.dataSource = self 
    dataSource = ["item1", "item2", "item3"] 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return dataSource.count 
    } 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell 

     cell.myLabel.text = dataSource[indexPath.row] 
     return cell 
    } 
    //Suppose in some function you changed the datasource 
    func updateData() { 
     dataSource = ["number1", "number2", "number3"] 
    //Now just call 
    myTableView.reloadData() 

} 
+0

아니요, 문제는 초기 데이터가 표시되지 않습니다. 초기 데이터를 표시하기 위해 다시로드 할 필요가 없습니다 ( – Danny

2

나는이 방법을 수행해야한다고 생각 : 사용자 정의 셀)

만들기 1

class customCell: UITableViewCell 
{ 
    @IBOutlet weak var myLabel: UILabel! 
} 

2)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! customCell 
    cell.myLabel.text = "1234" 
    return cell 
+0

) 아니요, MyTableViewCell이 이미 생성 된 것을 볼 수 있습니다. – Danny

0

재사용 셀과 cellForRowAtIndexPath에 식별자를 설정합니다 문제가있는 곳을 찾았습니다. 사실 내 UITableView는 tabBarController 내부에있는 UIViewController 내부에있었습니다. Xcode 7.3.1에서 런타임에 제약 조건이 적용되지 않는 이유가 여기에 있습니다.

모든 제약 조건과 셀의 UILabel에 대해 확인란을 선택하여을 true로 수정했습니다.

관련 문제