简体   繁体   English

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

[英]TableViewCell not displaying custom cell info

I have a tableview that gets its information from devices in persistent data store which is updated when something happens. 我有一个tableview,它从持久性数据存储中的设备获取其信息,该信息在发生某些情况时会更新。 This update works fine. 此更新工作正常。

Anyway I get the data and load it into my TableDataVainView.plist and have the tableView reload the data. 无论如何,我都会获取数据并将其加载到TableDataVainView.plist中,并让tableView重新加载数据。 Trouble is the cell lines are there but no data. 问题是细胞系在那里但没有数据。

I have checked that the data exists and it is being populated in the cell.textLabel and cell.detailTextLabel but again it doesn't appear. 我已经检查了数据是否存在,并在cell.textLabel和cell.detailTextLabel中填充了数据,但再次没有出现。

Here is my code: 这是我的代码:

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

Like I said this used to work fine but then it stopped working. 就像我说的那样,以前可以正常工作,但是后来停止工作了。
I do hard code some background views but not the tableView which is set in the IB. 我对某些背景视图进行硬编码,但对IB中设置的tableView进行硬编码。 But since I can see the cell division lines I'm thinking that the table view is on top. 但是,因为我可以看到单元格划分线,所以我认为表格视图位于顶部。 I have tied the datasource and delegate to the tableView. 我已将数据源绑定并委托给tableView。 Just not sure why it isn't displaying the cell info. 只是不确定为什么它不显示单元格信息。

Any help would be greatly appreciated. 任何帮助将不胜感激。

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

since my tableView is created in the IB it resides atIndex:0 由于我的tableView是在IB中创建的,因此它位于atIndex:0

By changing my code to put backgroundImage atIndex:-2 and backgroundImage2 atIndes:-1 My tableView now is displayed, but it has a black background so I can't see my two backgroundImages. 通过更改代码,将backgroundImage atIndex:-2和backgroundImage2 atIndes:-1一起显示,现在显示了tableView,但是它具有黑色背景,所以我看不到我的两个backgroundImages。

In the IB I have made the tableview background clear and in code as well as the cell's background but still there is a black background. 在IB中,我已使tableview背景清晰,并在代码以及单元格的背景中进行了显示,但是仍然有黑色背景。

Since this is still the problem of the tableView I thought I would amend this to see how to make the tableView background clear. 由于这仍然是tableView的问题,我想我将对其进行修改以查看如何使tableView背景清晰。 If I need to I can ask another question and mark this one done. 如果需要,我可以问另一个问题并将其标记为完成。

In your code, UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 在您的代码中, UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; This shows there is a lot of hassles as it is not prepared by yourself. 这表明有很多麻烦,因为它不是您自己准备的。 The returned cell may be not as same as the designed one. 返回的单元格可能与设计的单元格不同。

An easy quick fix to this is to use a customized UITableViewCell. 一个简单的快速解决方案是使用自定义的UITableViewCell。 Then register as the CellIdentifier in tableViewController. 然后在tableViewController中注册为CellIdentifier。 So you can customize in your nib/xib file especially if time is limited. 因此,您可以在nib / xib文件中进行自定义,尤其是在时间有限的情况下。

I noticed that your viewDidAppear method does not call: 我注意到您的viewDidAppear方法不会调用:

[super viewDidAppear:animated];

This may or may not be the underlying problem, but it will lead to undefined behaviour. 这可能是根本问题,也可能不是根本问题,但是它将导致不确定的行为。 Make that the first line in viewDidAppear . viewDidAppear第一行。

Another tip would be to get the misbehaving table view on the screen, and then use the 'debug view hierarchy' feature in Xcode. 另一个技巧是在屏幕上获取行为异常的表视图,然后在Xcode中使用“调试视图层次结构”功能。 It's like a breakpoint, but for views. 这就像一个断点,但需要查看。 You'll be able to see exactly what is on the screen and in what ordering. 您将能够准确地看到屏幕上的内容和顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM