iPhone UITableView with a header area

前端 未结 8 943
礼貌的吻别
礼貌的吻别 2020-12-23 20:26

I have a view that was created with all of the default UITableView stuff, but now I need to add a header area above where the UITableView is (so th

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 20:50

    You can use UITableViewDelegate methods to create a custom header view for a table and specify the height, namely tableView:viewForHeaderInSection: and tableView:heightForHeaderInSection:. You can add whatever you like to the view. Here's an example that adds a right aligned UILabel:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
    
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
    
        headerLabel.textAlignment = NSTextAlignmentRight;
        headerLabel.text = [titleArray objectAtIndex:section];
        headerLabel.backgroundColor = [UIColor clearColor];
    
        [headerView addSubview:headerLabel];
    
        return headerView;
    
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
        return  30.0;
    }
    

提交回复
热议问题