[ios] UITableView 헤더 섹션 사용자 정의

UITableView각 섹션의 헤더 를 사용자 정의하고 싶습니다 . 지금까지 구현했습니다

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

UITabelViewDelegate방법. 내가하고 싶은 것은 각 섹션에 대한 현재 헤더를 가져 와서 UILabel하위보기로 추가 하는 것입니다.

지금까지는 그렇게 할 수 없습니다. 기본 섹션 헤더를 가져올 항목을 찾을 수 없기 때문입니다. 첫 번째 질문 은 기본 섹션 헤더를 얻는 방법이 있습니까?

가능하지 않으면 컨테이너 뷰를 만들어야 UIView하지만 이번에는 기본 배경색, 그림자 색 등을 설정해야합니다. 섹션 헤더를주의 깊게 살펴보면 이미 사용자 정의되어 있기 때문입니다.

각 섹션 헤더에 대해 이러한 기본값을 어떻게 얻을 수 있습니까?

다들 감사 해요.



답변

당신은 이것을 시도 할 수 있습니다 :

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}


답변

선택한 답변 tableView :viewForHeaderInSection:이 정확합니다.

여기서 팁을 공유하십시오.

스토리 보드 / xib를 사용하는 경우 다른 프로토 타입 셀을 만들어 “섹션 셀”에 사용할 수 있습니다. 헤더를 구성하는 코드는 행 셀을 구성하는 방법과 유사합니다.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *HeaderCellIdentifier = @"Header";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HeaderCellIdentifier];
    }

    // Configure the cell title etc
    [self configureHeaderCell:cell inSection:section];

    return cell;
}


답변

Lochana Tejas 의 신속한 버전 :

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18))
    let label = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18))
    label.font = UIFont.systemFontOfSize(14)
    label.text = list.objectAtIndex(indexPath.row) as! String
    view.addSubview(label)
    view.backgroundColor = UIColor.grayColor() // Set your background color

    return view
}


답변

기본 헤더보기를 사용하는 경우 기본 헤더보기 만 사용하여 텍스트를 변경할 수 있습니다

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

스위프트

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

보기를 사용자 정의하려면 새보기를 직접 작성해야합니다.


답변

UITableViewHeaderFooterView를 사용하지 않습니까?


답변

headerInSection이 표시되지 않으면 시도해보십시오.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

주어진 섹션의 헤더 높이를 반환합니다.


답변

lochana 및 estemendoza의 신속한 3 버전 답변 :

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

    let view = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.size.width, height:18))
    let label = UILabel(frame: CGRect(x:10, y:5, width:tableView.frame.size.width, height:18))
    label.font = UIFont.systemFont(ofSize: 14)
    label.text = "This is a test";
    view.addSubview(label);
    view.backgroundColor = UIColor.gray;
    return view

}

또한 다음을 구현해야합니다.

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 100;
}