使用SegmentedControl,我在多个CustomCells上遇到了一个小问题。我已经导入了一个带有4个分段控件的框架HMSegmentedControl,如(Admin,Engineer,Doctor,Employee)。现在的问题是如何使用多个自定义单元格(AdminCellList,EngineerCellList,DoctorCellList,EmployeeListCell)。单击Admin SegmentedControl时,应加载AdminCellList,依此类推。以下是我的尝试。 TIA。
MYViewController.h
@interface MYViewController : UIViewController
{
NSUInteger currentScreen;
}
MYViewController.m
self.AdminView.hidden = NO;
self.EngineerView.hidden = YES;
self.DoctorView.hidden = YES;
self.EmployeeView.hidden = YES;
currentScreen=0;
[self.scrollView addSubview:self.AdminView];
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Admin", @"Engineer", @"Doctor", @"Employee"]];
segmentedControl.frame = CGRectMake(0, 0, viewWidth, 45);
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
segmentedControl.selectionIndicatorColor = [UIColor blackColor];
segmentedControl.selectionIndicatorHeight = 2.0f;
segmentedControl.verticalDividerEnabled = YES;
segmentedControl.verticalDividerColor = [UIColor blackColor];
segmentedControl.verticalDividerWidth = 1.0f;
segmentedControl.borderColor = [UIColor blackColor];
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return attString;
}];
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
{
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
//Loading Service
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
//Loading Service
}
else if(segment.selectedSegmentIndex == 2) {
currentScreen=2;
//Loading Service
}
else if(segment.selectedSegmentIndex == 3) {
currentScreen=3;
//Loading Service
}
}
- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
NSLog(@"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
答案 0 :(得分:0)
一切看起来都不错,只需要一些补充:
1.每次用户更改细分时都会显示表格。
2.根据cellForRowAtIndexPath
在currentScreen
内查看并使用自定义类加载相应的单元格。
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment {
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
}
....... more code
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
if (currentScreen == 0) {
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 1) {
//Same code for cell as AdminCellList
EngineerCellList *cell = (EngineerCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EngineerCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (EngineerCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 2) {
//Similar custom cell code here as above
} else {
//Similar custom cell code here as above
}
}