Home > Software design >  Custom UITableView not appearing on iPhone or iPad
Custom UITableView not appearing on iPhone or iPad

Time:09-16

It's an old app, and uses hard coded values, however, there is no display of the table at all - whilst I realise it would mean I need to hardcode each value for each device, at the moment I can't even see the tableview to start doing that.

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(300, 180, 236, 672) style:UITableViewStylePlain];
    else {
        myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(113, 100, 196, 372) style:UITableViewStyleGrouped];
    }
    [myCustomTableView setDelegate:self];
    [myCustomTableView setDataSource:self];
    [myCustomTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [myCustomTableView setSeparatorColor:[UIColor whiteColor]];
    myCustomTableView.opaque = NO;
    myCustomTableView.clipsToBounds = YES;
    myCustomTableView.layer.cornerRadius=8.0;
    [myCustomTableView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomTableView];

It's worked in older versions of iOS (perhaps iOS 10.0 downwards

Added extra coding here;

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifer=@"Cell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    if (cell == nil) {
        //cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifer] autorelease];
        NSLog(@" gell is allocating");
        cell =
        [[[UITableViewCell alloc]
          initWithFrame:CGRectZero
          reuseIdentifier:cellIndentifer]
         autorelease];
        
        UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 4, 35, 47)];
        cellImageView.tag = leftImageViewTag;
        [cellImageView setContentMode:UIViewContentModeScaleToFill];
        [cell.contentView addSubview:cellImageView];
        [cellImageView release];
        
        
        UIImageView *cellImageViewSubView = [[UIImageView alloc]initWithFrame:CGRectMake(13, 12,18 , 24)];
        cellImageViewSubView.tag = leftImageSubViewTag;
        [cellImageViewSubView setContentMode:UIViewContentModeScaleToFill];
        //[cellImageViewSubView setImage:[UIImage imageNamed:@"sp4.png"]];
        [cell.contentView addSubview:cellImageViewSubView];
        [cellImageViewSubView release];
        
        
        UIButton *myAddButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [myAddButton setFrame:CGRectMake(5, 10, 40, 40)];
        [myAddButton setTag:leftAddButtonTag];
        [myAddButton setHidden:YES];
        if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) {
            [myAddButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"add@2x" ofType:@"png"]] forState:UIControlStateNormal];

        }
        else {
            [myAddButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"add" ofType:@"png"]] forState:UIControlStateNormal];

        }

        [myAddButton addTarget:self action:@selector(addCellForCamera) forControlEvents:UIControlEventTouchUpInside];
        [myAddButton setTitle:@"" forState:UIControlStateNormal];
        [cell.contentView addSubview:myAddButton];
        
        UIButton *myMinusButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [myMinusButton setFrame:CGRectMake(140, 10, 40, 40)];
        [myMinusButton setTag:rightMinusButtonTag];
        if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) {
            [myMinusButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"minus@2x" ofType:@"png"]] forState:UIControlStateNormal];
            
        }
        else {
            [myMinusButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"minus" ofType:@"png"]] forState:UIControlStateNormal];
            
        }
        [myMinusButton setHidden:YES];
        [myMinusButton addTarget:self action:@selector(minusCellForCamera:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:myMinusButton];
        
        CameraButton *myCameraButton=[CameraButton buttonWithType:UIButtonTypeCustom];
        [myCameraButton setFrame:CGRectMake(135, 10, 50, 30)];
        [myCameraButton setTag:myCameraButtonTag];
        [myCameraButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"theme_Camera" ofType:@"png"]] forState:UIControlStateNormal];
        [myCameraButton setHidden:YES];
        [myCameraButton addTarget:self action:@selector(CellForCameraButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [myCameraButton setIndexpath:indexPath];
        [cell.contentView addSubview:myCameraButton];               
        
        
        UIImageView *cellImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(150, 4, 25, 25)];
        cellImageView2.tag = tickImageViewTag;
        [cellImageView2 setHidden:YES];
        [cellImageView2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"click" ofType:@"png"]]];
        [cellImageView2 setContentMode:UIViewContentModeCenter];
        [cell.contentView addSubview:cellImageView2];
        [cellImageView2 release];
        
        
        UILabel *mylabel=[[UILabel alloc] initWithFrame:CGRectMake(60, 5, 130, 30)];
        mylabel.tag=middleLabelTag;
        [mylabel setBackgroundColor:[UIColor clearColor]];
        mylabel.textColor=[UIColor whiteColor];
        
        [cell addSubview:mylabel];
        [mylabel release];
        cell.backgroundView =
        [[[UIImageView alloc] init] autorelease];
        
        
        
        UIImageView *backgroundSelection=[[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grey-hover" ofType:@"png"]]];
        cell.selectedBackgroundView=[backgroundSelection autorelease];
        
        
        
    }
    
    [(CameraButton *)[cell viewWithTag:myCameraButtonTag] setIndexpath:indexPath];
    UIImage *rowBackground;
    
    NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
    NSInteger row = [indexPath row];
    
    if (row == 0)
    {
        rowBackground = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"top-row" ofType:@"png"]];
        
    }
    else if (row == sectionRows - 1)
    {
        
        rowBackground = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"btm-row" ofType:@"png"]];
        
        
    }
    else
    {
        rowBackground = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"row" ofType:@"png"]];
        
        
    }
    ((UIImageView *)cell.backgroundView).image = rowBackground;
    
    if (self.tickCurrentPath == indexPath) {
        [[cell viewWithTag:tickImageViewTag] setHidden:NO];
    }
    else {
        [[cell viewWithTag:tickImageViewTag] setHidden:YES];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    
    if (indexPath.section == 0) {
        if (indexPath.row == [customArray count] ) {
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
            [[cell viewWithTag:leftAddButtonTag] setHidden:NO];
            [[cell viewWithTag:rightMinusButtonTag] setHidden:NO];
            [[cell viewWithTag:leftImageViewTag] setHidden:YES];
            [[cell viewWithTag:tickImageViewTag] setHidden:YES];
            
            [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
            
            if ([customArray count]==0) {
                UIButton *button=(UIButton *)[cell viewWithTag:rightMinusButtonTag];
                [button setEnabled:NO];         }
            else {
                UIButton *button=(UIButton *)[cell viewWithTag:rightMinusButtonTag];
                [button setEnabled:YES];
            }
            
            UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
            middleText.text=@"";
        }
        else {
            [[cell viewWithTag:myCameraButtonTag] setHidden:NO];
            
            [[cell viewWithTag:leftImageViewTag] setHidden:NO];
            [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
            [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
            
            //batter-sm-blnk.png
            UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
            [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"batter-sm-blnk" ofType:@"png"]]];
            
            UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
            NSDictionary *tempDict=[customArray objectAtIndex:indexPath.row];
            //  NSLog(@"tempdict contains %@",customArray);
            middleText.text=[tempDict objectForKey:@"Cell_Text"];
            
            NSData *imageData=[tempDict objectForKey:@"Image"];
            
            NSLog(@"size of image:  %d",[imageData length]);
            if ([imageData length]>1) {
                [[cell viewWithTag:leftImageSubViewTag] setHidden:NO];
                //NSData *imageData=[tempDict objectForKey:@"Image"];
                UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageSubViewTag];
                [batteryImageView setImage:[UIImage imageWithData:imageData]];
            }else {
                [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
            }
            
            
            
            
            
        }
        return cell;
    }
    else if (indexPath.section == 1) {
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[colorArray objectAtIndex:indexPath.row];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellImages_Battery objectAtIndex:indexPath.row] ofType:@"png"]]];
        
        return cell;
        
    }
    
    else if (indexPath.section == 2)  {
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        
        
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[flagImageArray objectAtIndex:indexPath.row] ofType:@"png"]]];
        
        
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[flagArray objectAtIndex:indexPath.row];
        return cell;
    }
    else if (indexPath.section == 3)  {
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        
        
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[flagImageArray objectAtIndex:indexPath.row 5] ofType:@"png"]]];
        
        
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[flagArray objectAtIndex:indexPath.row 5];
        return cell;
    }
    else if (indexPath.section == 4)  {
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        
        
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[flagImageArray objectAtIndex:indexPath.row 10] ofType:@"png"]]];
        
        
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[flagArray objectAtIndex:indexPath.row 10];
        return cell;
    }
    else if (indexPath.section == 5)  {
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        
        
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[flagImageArray objectAtIndex:indexPath.row 15] ofType:@"png"]]];
        
        
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[flagArray objectAtIndex:indexPath.row 15];
        return cell;
    }
    else if (indexPath.section == 6)  {
        [[cell viewWithTag:leftImageSubViewTag] setHidden:YES];
        
        [[cell viewWithTag:leftImageViewTag] setHidden:NO];
        [[cell viewWithTag:myCameraButtonTag] setHidden:YES];
        
        
        UIImageView *batteryImageView=(UIImageView *)[cell viewWithTag:leftImageViewTag];
        [batteryImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[flagImageArray objectAtIndex:indexPath.row 20] ofType:@"png"]]];
        
        
        [[cell viewWithTag:rightMinusButtonTag] setHidden:YES];
        [[cell viewWithTag:leftAddButtonTag] setHidden:YES];
        UILabel *middleText=(UILabel *)[cell viewWithTag:middleLabelTag];
        middleText.text=[flagArray objectAtIndex:indexPath.row 20];
        return cell;
    }
    
    return cell;
    
    
}

CodePudding user response:

As DonMag mentioned in the comments, at a minimum you need to include the two UITableViewDataSource methods numberOfRowsInSection and cellForRowAtIndexPath for any cells to be rendered for the tableview.

Are you adding myCustomTableView to the view hierarchy somehow somewhere else in your code (i.e. self.view addSubview:)?

Here's a very simple example using your code as a base for generating a test tableview programmatically:

#import "ViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (strong, nullable) UITableView *myCustomTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        self.myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(300, 180, 236, 672) style:UITableViewStylePlain];
    } else {
        self.myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(113, 100, 196, 372) style:UITableViewStyleGrouped];
    }
    [self.myCustomTableView setDelegate:self];
    [self.myCustomTableView setDataSource:self];
    [self.myCustomTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.myCustomTableView setSeparatorColor:[UIColor whiteColor]];
    self.myCustomTableView.opaque = NO;
    self.myCustomTableView.clipsToBounds = YES;
    self.myCustomTableView.layer.cornerRadius=8.0;
    [self.myCustomTableView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:self.myCustomTableView];
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Test"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Section: %li, Row: %li", indexPath.section, indexPath.row];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}



@end

I suspect your tableview is never being added to the view hierarchy, you can use the "Debug View Hierarchy" while running your application to see what views are in the view hierarchy. The button for it is right at the top of the debug area which is near the bottom of the Xcode window.Debug View Hierarchy Button

The output of the above sample code looks like this:

TableView Sample on iPhone Simulator

CodePudding user response:

OK, so it seems in my code, for some reason ViewWillAppear was not being called, so when switching to ViewDidLoad the tableview showed up. Thank you to all that have helped.

  • Related