Vấn đề là, thuộc tính chế độ xem của UITableViewController giống với thuộc tính tableView. Tôi đã có cùng một vấn đề, muốn đặt một số nội dung cố định phía trên bảng. Tôi không muốn thay đổi lớp cơ sở, vì nó cung cấp rất nhiều chức năng tuyệt vời mà tôi không muốn mất hoặc bị gián đoạn.
Việc sửa chữa thực sự dễ dàng. Bí quyết là tạo tập hợp tùy chỉnh và nhận được thuộc tính self.tableView. Sau đó, trong loadView, bạn thay thế khung nhìn bằng UIView mới và thêm tableView vào nó. Sau đó, bạn được tự do thêm các bản xem phụ xung quanh tableView. Đây là cách nó được thực hiện:
Trong tiêu đề:
@interface CustomTableViewController : UITableViewController
{
UITableView *tableView;
}
Trong .m:
- (UITableView*)tableView
{
return tableView;
}
- (void)setTableView:(UITableView *)newTableView
{
if (newTableView != tableView)
{
[tableView release];
tableView = [newTableView retain];
}
}
- (void)loadView {
[super loadView];
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[replacementView release];
[self.view addSubview:self.tableView];
//code below adds some custom stuff above the table
UIView *customHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
customHeader.backgroundColor = [UIColor redColor];
[self.view addSubview:customHeader];
[customHeader release];
self.tableView.frame = CGRectMake(0, customHeader.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - customHeader.frame.size.height);
}
Thưởng thức!
Nguồn
2012-01-31 17:41:21
Bản sao có thể có của [UITableView với tiêu đề phần cố định] (http://stackoverflow.com/questions/17582818/uitableview-with-fixed-section-headers) –