여러 UITABBAR 섹션에 대한 하나의 ViewController
-
06-07-2019 - |
문제
현재 데이터 표시를 위해 Uitabbar 및 테이블 뷰를 사용하여 간단한 iPhone 응용 프로그램을 개발하고 있습니다. Tabbar에는 8 개의 섹션이 포함되어 있으며 각각 동일한보기가 포함되어 있지만 다른 데이터 소스를 사용하면 테이블보기의 제목도 다르지만 뷰는 동일합니다.
최소한의 차이가있는 8 개의 개별 뷰 구성자를 작성하는 대신 데이터 소스 및 테이블 제목을 설정하는 옵션과 함께 각보기마다 동일한 뷰 콘트롤러를 사용하고 싶습니다. 그것이 가능할 것이고 더 중요한 것은 어떻게 이것이 가능합니까?
업데이트
Ole Begemann이 게시 한 코드를 사용하여 어느 정도 노력하고 있습니다. 그러나 나는 나의 초기 질문이 어떤 전문 용어를 섞었다는 것을 깨달았다. uitableviewdatasource는 공식 프로토콜이며 내가 달성하려는 일에 너무 많은 노력을 기울입니다. 테이블을 한 번만 설정하려면 논리 만 있으면 테이블보기를 채우는 데이터만이 다른 유일한 것입니다. "DataSource"가 의미하는 것은 내 ViewController의 UitableView 함수에서 사용할 수있는 객체의 사전 일뿐입니다.
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"Id",
@"The Name", @"Name",
@"Post", @"Type",
@"09-09-2009", @"Meta",
@"This is the excerpt, @"Excerpt",
@"This is the body text", @"Body",
@"icon.jpg", @"Icon",
@"image.jpg", @"Image", nil];
NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
이 사전을 대의원에서 uitableviewcontroller로 어떻게 전달할 수 있습니까?
해결책
처럼 다니엘은 말한다, 원하는 뷰 컨트롤러의 많은 인스턴스를 작성한 다음 이러한 인스턴스를 탭 막대 컨트롤러의 탭에 할당 할 수 있습니다.
코드는 다음과 같이 보일 수 있습니다 (여기서는 3 개의 인스턴스 만 만듭니다).
// Create an array of dictionaries that holds the configuration for the table view controllers.
// Assuming tableXDatasource are existing objects that conform to the UITableViewDataSource protocol.
NSArray *tableControllersConfig = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:table1Datasource, @"datasource", @"Table 1", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:table2Datasource, @"datasource", @"Table 2", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:table3Datasource, @"datasource", @"Table 3", @"title", nil],
nil];
// Create the table view controller instances and store them in an array
NSMutableArray *tableControllers = [NSMutableArray array];
for (NSDictionary *configDict in tableControllersConfig) {
// Assuming MyTableViewController is our custom table view controller class
MyTableViewController *controller = [[MyTableViewController alloc] initWithNibName:@"MyTableViewController" bundle:nil];
controller.tableView.delegate = controller;
controller.tableView.dataSource = [configDict valueForKey:@"datasource"];
controller.title = [configDict valueForKey:@"title"];
[tableControllers addObject:controller];
[controller release];
}
// Assign the array of table view controllers to the tab bar controller
self.tabBarController.viewControllers = [NSArray arrayWithArray:tableControllers];
다른 팁
가능하면, 다른 데이터 소스와 같은 UD만큼 View 컨트롤러를 인스턴스화 할 수 있습니다 ... 각 탭에 대한 뷰 컨트롤러가있는 것처럼 동일한 절차를 따를 것입니다 ...
원하는 버튼과 함께 UIToolbar를 사용하고 각 버튼에서 ReloadData를 호출 할 수 있습니다. 다른 매개 변수로 클릭하십시오.