2014-11-19 2 views
7

UITableView의 섹션에서 머리글 제목을 얻으려는 경우, 구문에서 구문은 무엇입니까? swift 섹션에서 머리글 제목을 얻으려고합니다.UITableView get titleForHeadersInSection swift

func tableView(tableView : UITableView, titleForHeaderInSection section: Int)->String 
{ 
    switch(section) 
    { 
    case 2:return "Title 2" 
     break 
    default :return "" 
     break 
    } 

} 

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
    { 

var title = tableView.titleForHeaderInSection[section]; 
if (title == "") { 
return 0.0; 
} 
return 20.0; 
} 

func tableView (tableView:UITableView, viewForHeaderInSection section:Int)->UIView 
{ 

var title = tableView.titleForHeaderInSection[section] as String 
if (title == "") { 
return UIView(frame:CGRectZero); 
} 
var headerView:UIView! = UIView (frame:CGRectMake(0, 0, self.tableView.frame.size.width, 20.0)); 
headerView.backgroundColor = self.view.backgroundColor; 

return headerView; 
} 
+0

이유 : 당신의 코드를 사용하여 : (섹션있는 tableView, titleForHeaderInSection) 예를 들어

,

self.tableView을 제목 대신 비교하기 위해 섹션 번호를 사용하지 않습니까? – zisoft

+0

실제로 Header In Section의 제목을 가져 오는 구문을 알고 싶습니다. –

답변

3

이 메서드를 호출하려면 UITableViews titleForHeaderInSection 메서드를 사용해야합니다. 이 메소드는 현재 섹션의 색인을 제공하며 String을 리턴해야합니다. 리턴 된 String은 헤더로 설정됩니다. 이를 호출하기 위해

, 우리는 단순히 이것은 위의 순서대로 섹션 제목을 반환합니다

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
{ 
    // Ensure that this is a safe cast 
    if let carsArray = cars as? [String] 
    { 
     return carsArray[section] 
    } 

    // This should never happen, but is a fail safe 
    return "unknown" 
} 

를 호출 할 수 있습니다 우리는 문자열의 배열 cars

cars = ["Muscle", "Sport", "Classic"] 

라는 있다고 가정 할 수 있습니다.

11

이미 클래스의 예에 정의 된 FUNC 사용할 수 있습니다

func tableView(tableView : UITableView, titleForHeaderInSection section: Int)->String { 
    switch(section) { 
    case 2:return "Title 2" 

    default :return "" 

    } 
} 

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
{ 

    var title = self.tableView(tableView, titleForHeaderInSection: section) 
    if (title == "") { 
     return 0.0 
    } 
    return 20.0 
} 
+0

감사합니다. BTW, 당신은 "휴식"이 필요하지 않습니다. –

+1

큰 스 니펫의 잘라 붙여 넣기에서 남은 '나누기 - 제거됨 :) – HungryArthur

관련 문제