该应用可以让用户将商品标记为收藏。当用户没有保存任何收藏夹时,我想通知他们这一事实(大多数情况下,我讨厌空白的tableView的想法)。
当没有用户标记为收藏夹的项目时,我的numberOfRowsInSection
为零。我想设置cell.textLabel.text = @“您没有收藏夹”,但是当表中没有任何项目时,就不会调用cellForRowAtIndexPath
。
我可以测试numberOfRowsInSection
在遇到0时给出的结果,然后测试cellForRowAtIndexPath
中的1行,然后插入自定义文本,但是如果它们只有一个收藏夹怎么办?
更新
我尝试实现上面的想法,并在下面推荐了它,但是也许我做的不正确,这可能是因为它是一个fetchedResultsController,带有委托(delegate)方法,可以在发生更改时更新表。
当表中只有一个单元格时,删除单元格时会出现此错误:*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null)
当没有单元格显示在第一位置时,它将崩溃:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0)'
以下是相关代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
if ([sectionInfo numberOfObjects]==0) {
return 1;
} else {
return [sectionInfo numberOfObjects];
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
if ([[_fetchedResultsController sections] objectAtIndex:0]==0) {
cell.textLabel.text = @"You have no favorites";
} else {
Shows *show = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = show.ShowsToSerials.serialName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", show.showTitle];
}
}
最佳答案
1)在我自己的iOS应用中,我创建一个带有UILabel的 subview ,并说“无结果”
当numberOfSectionsInTableView为零时,我将该 subview 添加到tableview中。当它等于零时,我将其删除。
当然,这需要将 subview 作为保留对象保留在内存中,以便可以将其删除。
2)如果您想像您提到的那样制作一个单元格:
当numberOfSectionsInTableView为零时,返回1。然后,在numberOfRowsInSection中也返回1。
在cellForRowAtIndexPath中,检查numberOfSectionsInTableView和numberOfRowsInSection,如果它们应该为零(毕竟返回了1),则显示任何消息。
解决方案1需要维护一个变量来保存 subview ,但是代码要短一些,代码更简洁,而且我认为CPU占用较少(这不会导致任何严重的性能问题)。
编辑以包含代码:
实际上,这与我想像的有所不同,但基本相同。
显然,这里有些事情需要更改以适合您的代码和口味。
(确保确保声明 UIView * nomatchesView;在.h中声明)
- (void)viewDidLoad{
nomatchesView = [[UIView alloc] initWithFrame:self.view.frame];
nomatchesView.backgroundColor = [UIColor clearColor];
UILabel *matchesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,320)];
matchesLabel.font = [UIFont boldSystemFontOfSize:18];
matchesLabel.minimumFontSize = 12.0f;
matchesLabel.numberOfLines = 1;
matchesLabel.lineBreakMode = UILineBreakModeWordWrap;
matchesLabel.shadowColor = [UIColor lightTextColor];
matchesLabel.textColor = [UIColor darkGrayColor];
matchesLabel.shadowOffset = CGSizeMake(0, 1);
matchesLabel.backgroundColor = [UIColor clearColor];
matchesLabel.textAlignment = UITextAlignmentCenter;
//Here is the text for when there are no results
matchesLabel.text = @"No Matches";
nomatchesView.hidden = YES;
[nomatchesView addSubview:matchesLabel];
[self.tableView insertSubview:nomatchesView belowSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//If there is no table data, unhide the "No matches" view
if([data count] == 0 ){
nomatchesView.hidden = NO;
} else {
nomatchesView.hidden = YES;
}
return [data count];
}
关于iphone - 当没有数据可用于tableView时显示自定义标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4840591/