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



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

无论如何,我获取数据并将其加载到我的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放在索引:-2和背景图像2在Indes:-1现在显示了我的tableView,但它有一个黑色背景,所以我看不到我的两个背景图像。

在IB中,我已经在代码中清楚地显示了表视图背景以及单元格的背景,但仍然有黑色背景。

由于这仍然是tableView的问题,我想我应该对此进行修改,看看如何使tableView背景清晰。如果需要的话,我可以再问一个问题,并标记这个问题已经完成。

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

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

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

[super viewDidAppear:animated];

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

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

相关内容

最新更新