【问题标题】:Custom UITableView not appearing on iPhone or iPad自定义 UITableView 未出现在 iPhone 或 iPad 上
【发布时间】:2021-09-10 13:59:46
【问题描述】:

这是一个旧应用程序,使用硬编码值,但是,根本没有显示表格 - 虽然我意识到这意味着我需要为每个设备硬编码每个值,目前我什至不能请参阅 tableview 以开始执行此操作。

-(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];

它适用于旧版本的 iOS(可能是 iOS 10.0 以下版本

在此处添加了额外的编码;

-(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;
    
    
}

【问题讨论】:

  • 做一点调试... numberOfRowsInSectioncellForRowAtIndexPath 被调用了吗?如果您使用 Debug View Hierarchy,您的表视图是否存在?它有细胞吗?是否存在,但以某种方式隐藏或定位在屏幕外?
  • 你在做某事,它似乎没有被调用
  • viewWillAppear 是一个奇怪的地方来设置你的表格视图。如果这就是您的全部代码,那么您永远不会将表视图添加到视图层次结构中。你的[self.view addSubview: myCustomTableView]; 在哪里?
  • 对不起,我忘了补充,它实际上是在上面的最后一行之后(我已经编辑了代码),所以它就在那里,只是不工作:/
  • 好的 - 我觉得很傻,但我已经转移到 viewDidLoad 并且它有效....!非常感谢 DonMag

标签: ios objective-c


【解决方案1】:

正如 DonMag 在 cmets 中提到的,您至少需要包含两个 UITableViewDataSource 方法 numberOfRowsInSectioncellForRowAtIndexPath,以便为表格视图呈现任何单元格。

您是否将 myCustomTableView 添加到代码中其他地方的视图层次结构中(即 self.view addSubview:)?

这是一个非常简单的示例,使用您的代码作为以编程方式生成测试表视图的基础:

#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

我怀疑您的 tableview 从未添加到视图层次结构中,您可以在运行应用程序时使用“调试视图层次结构”来查看视图层次结构中的视图。它的按钮位于调试区域的顶部,靠近 Xcode 窗口的底部。

上述示例代码的输出如下所示:

【讨论】:

  • 谢谢,我已经发了cellForRowAtIndexPath
  • 使用该代码 - 它也不显示表格:(
  • 当您使用“调试视图层次结构”按钮作为答案中的屏幕截图时,表格视图是否显示?
  • 否 - 使用调试视图层次结构也不会显示
  • 您是在创建一个新项目并将该代码插入到已创建的默认 ViewController 中,还是尝试将其插入到您现有的项目中。如果您正在做后者,我建议您尝试做前者以确认它在那里为您工作。如果是这样,那么您可能需要进一步研究您自己的项目设置。
【解决方案2】:

好的,所以在我的代码中,由于某种原因,ViewWillAppear 没有被调用,所以当切换到ViewDidLoad 时,表格视图出现了。 感谢所有帮助过的人。

【讨论】:

  • 既然您说您正在将表格视图添加到层​​次结构中,那么未填充行的最可能原因是,如果您在viewWillAppear需要致电.reloadData()。但是,执行所有这些操作的正确位置是viewDidLoad
  • 这很有意义@DonMag
猜你喜欢
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多