UITableViewCell 不显示自定义单元格信息

Posted

技术标签:

【中文标题】UITableViewCell 不显示自定义单元格信息【英文标题】:TableViewCell not displaying custom cell info 【发布时间】:2018-11-06 20:28:49 【问题描述】:

我有一个 tableview,它从持久数据存储中的设备获取其信息,当发生某些事情时会更新。此更新运行良好。

无论如何,我获取数据并将其加载到我的 TableDataVainView.plist 并让 tableView 重新加载数据。问题是有细胞系但没有数据。

我已检查数据是否存在,并且已填充到 cell.textLabel 和 cell.detailTextLabel 中,但它再次没有出现。

这是我的代码:

- (NSManagedObjectContext *)managedObjectContext

    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) 
        context = [delegate managedObjectContext];
    
    return context;


- (void)viewDidAppear:(BOOL)animated
 // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Cell"];
    self.tableDataMainView = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cell"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error;
    NSArray *items = [self.managedObjectContext
                      executeFetchRequest:fetchRequest error:&error];
    NSManagedObjectContext *context = [self managedObjectContext];

    for (context in items) 
        NSLog(@"Title: %@,", [context name]);
    
    cellNamesArray = [items valueForKey:@"image"];
    NSLog(@"cellNameArray;%@",cellNamesArray);

    [self.tableView reloadData];


- (void)viewDidLoad 
    [super viewDidLoad];

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];

self.tableView.contentInset = UIEdgeInsetsMake(0, -10, 0, 0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *slasha = [documentsDirectory stringByAppendingPathComponent:@"/Names"];
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:slasha error:nil];

    NSString *slashb = [documentsDirectory stringByAppendingPathComponent:@"/Images"];
    NSArray * directoryContentsImages = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:slashb error:nil];

    int pictureCount;

    for (pictureCount = 0; pictureCount < (int)[directoryContentsImages count]; pictureCount++)
    

        NSLog(@"File %d: %@", (pictureCount + 1), [directoryContentsImages objectAtIndex:pictureCount]);
    

    NSLog(@"count:%d,",count);
    NSLog(@"picturecount:%d,",pictureCount);
for (count = 0; count < (int)[directoryContents count]; count++)
    

        NSLog(@"File %d: %@", (count + 1), [directoryContents objectAtIndex:count]);
    

    NSLog(@"count:%d,",count);
    Names = [NSArray arrayWithArray:directoryContents];

    NSLog(@"namesArray;%@",Names);
    if (count == 0) 

    
    else if (count == 1) 
        nameOne = [Names objectAtIndex:0];
        NSLog(@"nameOne;%@",nameOne);
        NSArray *coorArray = [nameOne componentsSeparatedByString:@","];
        NSString *firstString = [coorArray objectAtIndex:0];
        NSString *secondString = [coorArray objectAtIndex:1];
        NSLog(@"firstString;%@",firstString);
        NSLog(@"secondString;%@",secondString);
    
    else if (count == 2) 
        nameOne = [Names objectAtIndex:0];
        NSString *nameTwo =[Names objectAtIndex:1];
        NSArray *coorArray = [nameOne componentsSeparatedByString:@","];
        NSString *firstString = [coorArray objectAtIndex:0];
        NSString *secondString = [coorArray objectAtIndex:1];
        NSLog(@"firstString;%@",firstString);
        NSLog(@"secondString;%@",secondString);
        NSArray *coorArray2 = [nameTwo componentsSeparatedByString:@","];
        NSString *firstString2 = [coorArray2 objectAtIndex:0];
        NSString *secondString2 = [coorArray2 objectAtIndex:1];
        NSLog(@"firstString2;%@",firstString2);
        NSLog(@"secondString2;%@",secondString2);
    

    else if (count == 3) 
        nameOne = [Names objectAtIndex:0];
        NSString *nameTwo =[Names objectAtIndex:1];
         NSString *nameThree =[Names objectAtIndex:1];
        NSArray *coorArray = [nameOne componentsSeparatedByString:@","];
        NSString *firstString = [coorArray objectAtIndex:0];
        NSString *secondString = [coorArray objectAtIndex:1];
        NSLog(@"firstString;%@",firstString);
        NSLog(@"secondString;%@",secondString);
        NSArray *coorArray2 = [nameTwo componentsSeparatedByString:@","];
        NSString *firstString2 = [coorArray2 objectAtIndex:0];
        NSString *secondString2 = [coorArray2 objectAtIndex:1];
        NSLog(@"firstString2;%@",firstString2);
        NSLog(@"secondString2;%@",secondString2);
        NSArray *coorArray3 = [nameThree componentsSeparatedByString:@","];
        NSString *firstString3 = [coorArray3 objectAtIndex:0];
        NSString *secondString3 = [coorArray3 objectAtIndex:1];
        NSLog(@"firstString3;%@",firstString3);
        NSLog(@"secondString3;%@",secondString3);
    

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    NSLog(@"number of sections called");
    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
   NSLog(@"got row count");

    return self.tableDataMainView.count;

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
    NSLog(@"index path");


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

   NSLog(@"Cell");

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    device = [self.tableDataMainView objectAtIndex:indexPath.row];
    [cell.textLabel setText:[device valueForKey:@"name"]];
    NSLog(@"celltextlabel%@",cell.textLabel.text);
    NSLog(@"deviceName???%@",device);
    cell.detailTextLabel.frame = CGRectMake(16, 35, 200, 65);
    [cell.detailTextLabel setText:[device valueForKey:@"winloss"]];
    NSLog(@"celldetailtextlabel%@",cell.detailTextLabel.text);
    NSLog(@"devicewinloss???%@",device);

   [cell setBackgroundColor:[UIColor clearColor]];
    UIView *myBackView = [[UIView alloc] init];
    myBackView.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = myBackView;
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
    NSString *slash3 = [documentsDirectory1 stringByAppendingPathComponent:@"/Images"];


    NSString *fullPath1 = [slash3 stringByAppendingPathComponent:[device valueForKey:@"image"]];

    //Now load the image at fullPath and install it into our image view's image property.
    if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath1])

    
    else 

        cell.imageView.image = [UIImage imageWithContentsOfFile: fullPath1];
        NSLog(@"fullPath1:%@",fullPath1);
    

    cell.imageView.layer.cornerRadius = 40;
    cell.imageView.clipsToBounds = YES;

    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell.detailTextLabel setTextColor:[UIColor whiteColor]];

    return cell;
 

就像我说的那样,这曾经可以正常工作,但后来就停止了工作。 我确实对一些背景视图进行了硬编码,但没有对 IB 中设置的 tableView 进行硬编码。但因为我可以看到细胞分裂线,所以我认为表格视图位于顶部。我已将数据源和委托绑定到 tableView。只是不知道为什么它不显示单元格信息。

任何帮助将不胜感激。

So as I came to suspect I have two backgrounds that are put in place by code ie.
[self.view insertSubview:backgroundImage atIndex:0];

    backgroundImage.image = [UIImage imageNamed:@"tilebackground long.png"];

self.backgroundImage2.image = [UIImage imageNamed:@"just stars long.png"];
    backgroundImage2.hidden = NO;
    [self.view insertSubview:backgroundImage2 atIndex:1];

因为我的 tableView 是在 IB 中创建的,所以它位于 Index:0

通过将我的代码更改为将 backgroundImage atIndex:-2 和 backgroundImage2 atIndes:-1 我的 tableView 现在显示了,但它的背景是黑色的,所以我看不到我的两个 backgroundImage。

在 IB 中,我已经在代码中以及单元格的背景中明确了 tableview 背景,但仍然有黑色背景。

由于这仍然是 tableView 的问题,我想我会修改它以查看如何使 tableView 背景清晰。如果需要,我可以提出另一个问题并将其标记为已完成。

【问题讨论】:

【参考方案1】:

在您的代码中, UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 这表明有很多麻烦,因为它不是自己准备的。返回的单元格可能与设计的单元格不同。

一个简单的快速解决方法是使用自定义的 UITableViewCell。然后在tableViewController中注册为CellIdentifier。因此,您可以在您的 nib/xib 文件中进行自定义,尤其是在时间有限的情况下。

【讨论】:

【参考方案2】:

我注意到您的 viewDidAppear 方法没有调用:

[super viewDidAppear:animated];

这可能是也可能不是根本问题,但它会导致未定义的行为。将其设为viewDidAppear 中的第一行。

另一个技巧是在屏幕上获取行为不端的表格视图,然后使用 Xcode 中的“调试视图层次结构”功能。这就像一个断点,但用于视图。您将能够准确地看到屏幕上的内容和顺序。

【讨论】:

以上是关于UITableViewCell 不显示自定义单元格信息的主要内容,如果未能解决你的问题,请参考以下文章

自定义 UITableViewCell 不显示内容

自定义 UITableViewCell 显示不正确的值

多个自定义 UITableViewCell 相互覆盖 Swift

我的自定义 UITableViewCell 表格单元格显示为项目的空白列表

在 iOS 中使用自定义单元格时无法查看 UITableViewCell

确保每个自定义 UITableViewCell 调整单元格高度?