UITableView
의 인스턴스를 하나만 유지 관리하고 탭 표시 줄을 사용하여 사용할 데이터 소스를 결정할 수 있습니다. 다음은 작업 표시 줄 컨트롤러의 탭 모음과 단일 tableview를 포함하는 스토리 보드의보기 컨트롤러 설정 예제입니다.
#import "ViewController.h"
@interface ViewController() <UITableViewDataSource, UITabBarDelegate>
@property(weak,nonatomic) IBOutlet UITableView *tableView;
@property(weak,nonatomic) IBOutlet UITabBar *tabBar;
@property(strong,nonatomic) NSArray *model;
@end
우리 모델은 탭 모음의 각 탭마다 하나씩 3 개의 배열입니다. 이 배열은 여기서 하드 코드 초기화되지만 웹 서비스에서 초기화 될 수 있습니다 ...
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *dataSource0 = @[ @"A", @"B", @"C" ];
NSArray *dataSource1 = @[ @"1", @"2", @"3"];
NSArray *dataSource2 = @[ @"doh", @"ray", @"me"];
self.model = @[ dataSource0, dataSource1, dataSource2 ];
self.tabBar.selectedItem = self.tabBar.items[0];
}
// the user pressed a tab, reload the table
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[self.tableView reloadData];
}
// this is an important part: our data for the table is determined by
// which tab is selected, return of the arrays, corresponding to the selection
- (NSArray *)currentDataSource {
UITabBarItem *item = self.tabBar.selectedItem;
NSInteger index = [self.tabBar.items indexOfObject:item];
return self.model[index];
}
// these datasource methods always refer to the currentDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self currentDataSource].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSString *text = [self currentDataSource][indexPath.row];
cell.textLabel.text = text;
return cell;
}
@end
@VladHatko 음, 동일한 테이블 뷰를 사용하여 두 세트의 데이터 그것으로. – JAL
@Rohit palod 여러 tableView를 만들면 메모리가 둘 이상이라고 생각해야합니다. 하나의 tableView를 사용하면 항목을 전환 할 때 여러 개의 dataSources를로드 할 수 있습니다. 여러 tableView를 사용하여 추천하지 않습니다,하지만 당신은 또한 귀하의 요구 사항을 relize하기 위해 isHidden을 설정할 수 있습니다. – aircraft
TableViews는 UI 요소입니다. 당신은'UITableViewDataSource'를 사용하여 테이블에 대한 데이터와 커스터마이징을 제공한다. 두 화면이 tableView에서 사용하는 데이터 바깥에서 모두 똑같다면 테이블 하나를 사용하고 테이블이 사용하는 데이터를 조작해야합니다. – Connor