2016-07-24 3 views
0

사용자 정의 셀 클래스에는 각 셀의 detailTextLabel에 "Jim"이라는 제목이있는 override func layoutSubviews()가 있습니다. DidSelectRowAtIndexPath를 클릭하면 셀의 세부 텍스트를 영구적으로 변경하여 (셀이 끊임없이 세부 정보를 작성하지 못하도록), "Bob"이라고 말하게 할 수 있습니까? DidSelectRowAtIndexPath 내에서 사용자 정의 셀을 수정하는 방법

답변

0

매우 간단

//This function is in the custom cell UserCell 
 

 
class CustomCell: UITableViewCell { 
 
override func layoutSubviews() { 
 
     super.layoutSubviews() 
 
     
 

 
     
 
      detailTextLabel?.text = "Jim" 
 
    } 
 

 
///........ 
 
} 
 

 

 
//In the viewController with the tableView outlet 
 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
 

 
//..... 
 

 

 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! CustomCell 
 
     
 
     //...... 
 
     
 
     
 
     return cell 
 
    } 
 

 

 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
 

 
/* Code I need to convert the detailTextLabel.text to equal "Bob" upon clicking on certain cell */ 
 

 
}

이처럼 수행

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

detailTextLabel?.text = "Bob" 

} 
+0

답변을 주셔서 감사합니다. 이미 그것을 사용해 봤지만, 오버라이드 func layoutSubviews는 jView로 다시로드 할 때 호출됩니다. 따라서 didSelectRow에서 Bob으로 선언 한 후에도 Jim으로 다시로드됩니다. ... – slimboy

0

자체가 어떤 데이터 상태를 유지하기 만를 표시하기 위해 사용되지 않아야 전지. 언더 레이 데이터 (문자열)를 유지하기 위해 컨트롤러에 가변 배열 속성을 만듭니다. 이 배열을 읽어 새 셀의 텍스트 속성을 설정하고 tableView:didSelectRowAtIndexPath:의 배열에서 "Bob"에 대한 인덱스의 값을 "Jim"으로 변경합니다. tableView가 다시로드 될 때마다 이제 dataSource에서 업데이트 된 값을 읽습니다.

UITableViewDelegate 프로토콜 외에도 UITableViewDataSource 프로토콜을 연구하십시오. 기본적으로 UITableViewController 클래스는이 두 프로토콜을 모두 준수하며 .tableView 속성에 각각 할당됩니다. self.tableView.delegateself.tableView.datasource 값을 인트로 스 케이트하면 원본 UITableViewController가 반환됩니다. UIViewController에서 상속받은 자신 만의 tableview 컨트롤러 클래스를 수동으로 만든 경우 제대로 작동하려면 tableView에 이러한 두 속성을 모두 지정해야합니다.