问题
I create a UITableView programmatically with different cells and sections that connects to the other views in storyboard
But if you check the story board absence view has custom cell with check box sections that it's not appear here
My question is:
why it doesn't shows the custom cell?,would you please helping me
Thanks in advance!
Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"WorkTime"]){
[segue.destinationViewController setTitle:@"WorkTime"];
}if([segue.identifier isEqualToString: @"Absence"]){
[segue.destinationViewController setTitle:@"Absence"];
}if([segue.identifier isEqualToString: @"Compensation"]){
[segue.destinationViewController setTitle:@"Compensation"];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *workKey = @"work";
NSString *absKey = @"absence";
NSString *comKey = @"compensation";
[contents setObject:[NSArray arrayWithObjects:@"Work Time", nil] forKey:workKey];
[contents setObject:[NSArray arrayWithObjects:@"Absence", nil] forKey:absKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", nil] forKey:comKey];
[keys addObject:workKey];
[keys addObject:absKey];
[keys addObject:comKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
//case1
[self performSegueWithIdentifier:@"WorkTime" sender:self];
//case2
[self performSegueWithIdentifier:@"Absence" sender:self];
//case3
[self performSegueWithIdentifier:@"Compensation" sender:self];
}
回答1:
I think your problem is here
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Your custom cell should have a unique cell identifier. You need to pass that unique cell identifier to -dequeueReusableCellWithIdentifier:.
What have you used as your cell identifier?
回答2:
you should use NSLOG to be sure that you are going to different views, I think you are always in a same view is the reason that you cann't see the new changes
来源:https://stackoverflow.com/questions/11899355/custom-cell-not-appear-when-loading-new-views-objective-c