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

时间:2018-11-06 20:28:50

标签: ios objective-c uitableview nsmanagedobjectcontext

我有一个表格视图,该表格视图从持久性数据存储中的设备获取其信息,该信息在发生某些情况时会更新。此更新工作正常。

无论如何,我都会获取数据并将其加载到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中创建的,所以它位于atIndex:0

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

在IB中,我已经使表格视图的背景清晰,代码以及单元格的背景清晰可见,但是仍然有黑色背景。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

[super viewDidAppear:animated];

这可能是根本问题,也可能不是根本问题,但这将导致不确定的行为。使该行成为viewDidAppear的第一行。

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