2009-08-28 38 views
29

Tôi biết cách tùy chỉnh tableViewCell.Làm thế nào để tùy chỉnh tableView Section View - iPhone

Tôi đã thấy nhiều ứng dụng tùy chỉnh ô TableView.

Tương tự như vậy, tôi muốn tùy chỉnh TableView Tiêu đề của Phần

"Giả - Một tên phần phải ở trong phông chữ khác nhau, Nó có ảnh nền khác nhau vv"

Có thể thực hiện như thế nào?

Tôi nên triển khai mã nào trong phương pháp nào?

+2

1 cho câu hỏi tốt đẹp. :) – mAc

Trả lời

60

Thay vì sử dụng các phương pháp thông thường

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

Bạn muốn thực hiện điều này một:

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

Như bạn thấy, điều thứ hai trả về một UIView thay vì chỉ chuỗi văn bản. Vì vậy, bạn có thể tùy chỉnh chế độ xem của riêng bạn (với nhãn vv) và trả lại.

Dưới đây là một ví dụ về cách bạn có thể làm điều đó (được thực hiện trong các phương pháp trên):

// create the parent view that will hold header Label 
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease]; 

// create image object 
UIImage *myImage = [UIImage imageNamed:@"someimage.png"];; 

// create the label objects 
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
headerLabel.backgroundColor = [UIColor clearColor]; 
headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
headerLabel.frame = CGRectMake(70,18,200,20); 
headerLabel.text = @"Some Text"; 
headerLabel.textColor = [UIColor redColor]; 

UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
detailLabel.backgroundColor = [UIColor clearColor]; 
detailLabel.textColor = [UIColor darkGrayColor]; 
detailLabel.text = @"Some detail text"; 
detailLabel.font = [UIFont systemFontOfSize:12]; 
detailLabel.frame = CGRectMake(70,33,230,25); 

// create the imageView with the image in it 
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease]; 
imageView.frame = CGRectMake(10,10,50,50); 

[customView addSubview:imageView]; 
[customView addSubview:headerLabel]; 
[customView addSubview:detailLabel]; 

return customView; 

Hy vọng rằng sẽ giúp

+1

Nếu bảng của bạn có hai phần chẳng hạn, bạn sẽ tham khảo phần bảng thứ hai như thế nào, vì vậy phần đầu trang 1 sẽ có headerLabel.text khác với tiêu đề phần 2? – iamtoc

+1

Bằng số nguyên được chuyển vào phương thức. –

+2

Nếu tôi muốn chỉ có tiêu đề cho phần = 1 chứ không phải cho phần = 0 thì sao? Tôi gặp sự cố với sự cố này ... – jsetting32

Các vấn đề liên quan