iOS UITableView 머리글 보기 추가(섹션 머리글이 아님)
연락처 앱과 같은 테이블 헤더(섹션 헤더가 아님)를 추가하려고 합니다. 예:
표 위의 이미지 옆에 있는 레이블입니다.
테이블 밖에 배치할 수 없도록 모든 보기를 스크롤할 수 있도록 합니다.
내가 어떻게 그럴 수 있을까?
UITableView
을 가지고 있습니다.tableHeaderView
소유물.원하는 보기로 설정합니다.
새 항목 사용UIView
컨테이너로 텍스트 레이블 및 이미지 보기를 새 항목에 추가합니다.UIView
그 다음 세트tableHeaderView
새로운 시각으로
예를 들어, 다음과 같은 경우UITableViewController
:
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
Interface Builder에서 매우 쉽게 수행할 수 있습니다.테이블이 있는 보기를 만들고 다른 보기를 테이블 위에 놓기만 하면 됩니다.테이블 머리글 보기가 됩니다.해당 보기에 레이블과 이미지를 추가합니다.보기 계층은 아래 그림을 참조하십시오.
Swift에서:
override func viewDidLoad() {
super.viewDidLoad()
// We set the table view header.
let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
self.tableView.tableHeaderView = cellTableViewHeader
// We set the table view footer, just know that it will also remove extra cells from tableview.
let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
self.tableView.tableFooterView = cellTableViewFooter
}
또한 Interface Builder에서 UIView만 만들고 ImageView 및 UI 레이블을 드래그 앤 드롭하여 원하는 헤더처럼 만들 수 있습니다.
UIV 보기가 원하는 방식으로 표시되면 XIB에서 프로그래밍 방식으로 초기화하고 UI 테이블 보기에 추가할 수 있습니다.즉, IB에서 전체 테이블을 설계할 필요가 없습니다.헤더 뷰만(이 방법으로 헤더 뷰를 다른 테이블에서도 재사용할 수 있음)
예를 들어 테이블 헤더 중 하나에 대한 사용자 정의 UI 보기가 있습니다.보기는 "사용자 정의"라는 xib 파일에 의해 관리됩니다.HeaderView"는 내 UITableViewController 하위 클래스에서 다음 코드를 사용하여 테이블 헤더에 로드됩니다.
-(UIView *) customHeaderView {
if (!customHeaderView) {
[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView" owner:self options:nil];
}
return customHeaderView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the CustomerHeaderView as the tables header view
self.tableView.tableHeaderView = self.customHeaderView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = @"LeadCode ";
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel1.textAlignment = NSTextAlignmentRight;
headerLabel1.text = @"LeadName";
headerLabel.textColor=[UIColor whiteColor];
headerLabel1.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel1];
return headerView;
}
언급URL : https://stackoverflow.com/questions/5441938/adding-ios-uitableview-headerview-not-section-header
'programing' 카테고리의 다른 글
루비로 해시 배열을 정렬하는 방법 (0) | 2023.06.01 |
---|---|
SDK 위치를 찾을 수 없습니다.local.properties 파일의 sdk.dir 또는 Android_HOME 환경 변수를 사용하여 위치를 정의합니다. (0) | 2023.06.01 |
Mac OS X에서 정적 라이브러리(.a)의 대상 아키텍처를 어떻게 결정합니까? (0) | 2023.06.01 |
HttpContext가 있는 웹 메서드입니다.현재의.사용자. 신원.Azure에서 비활성화 후 인증된 작업 중지 (0) | 2023.06.01 |
Android Emulator에서 내 http://localhost 웹 서버에 연결하는 방법 (0) | 2023.06.01 |