当没有可用于tableView的数据时显示自定义标签 [英] Show custom label when no data available for tableView
问题描述
相关应用可让用户将商品标记为收藏。当用户没有保存收藏夹时,我想通知他们这个事实(大多数我讨厌空白tableView的想法)。
The app in question has an ability for users to mark items as favourite. When the user has no favourites saved, I'd like to notify them of the fact (mostly I hate the idea of a blank tableView).
当用户没有标记为收藏的项目时,我的 numberOfRowsInSection
为零。我想设置cell.textLabel.text = @你没有收藏夹,但是当没有表的项目时,不会调用cellForRowAtIndexPath
。
My numberOfRowsInSection
is zero when there are no items the user has marked as favourite. I'd like to set cell.textLabel.text = @"You have no favourites" but when there are no items for the table cellForRowAtIndexPath
is not called.
我可以测试 numberOfRowsInSection
在遇到0时给出结果然后在中测试1行cellForRowAtIndexPath
然后插入自定义文本,但如果他们只有一个收藏夹会怎样?
I could test numberOfRowsInSection
to give a result when it encounters a 0 and then test for 1 row in cellForRowAtIndexPath
and then insert the custom text but then what if they only have one favourite?
更新
我尝试实现上面的想法,并在下面推荐,但也许我做得不对,可能是因为它是一个fetchedResultsController,其中委托方法在发生更改时更新表。
I tried implementing the idea I had above, and recommended below, but perhaps I'm not doing it right, probably due to the fact that it's a fetchedResultsController with the delegate methods to update the table when a change occurs.
当我在表格中只有一个单元格时删除单元格时出现此错误:
I get this error when I delete the cell when there is but one cell in the table:
***断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-1447.6.4 / UITableView.m:976
严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常。无效更新:第0节中的行数无效。更新(1)后现有部分中包含的行数必须等于更新前该部分中包含的行数(1),加上或减去数字从该部分插入或删除的行数(插入0,删除1)。 with userInfo(null)
当第一个地方没有要显示的单元格时,它会崩溃:
and when there are no cells to be displayed in the first place it crashes with:
***由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [_ PFBatchFaultingArray objectAtIndex:]:索引(0)超出边界(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的子视图,说没有结果
当numberOfSectionsInTableView为零时,我将子视图添加到tableview。当它是!=零时,我将其删除。
这当然要求将子视图作为保留对象保留在内存中,以便您可以将其删除。
1) In my own iOS apps, I create a subview with a UILabel in it, saying "No Results" When numberOfSectionsInTableView is zero, I add the subview to the tableview. When it is != zero, I remove it. This of course requires maintaining the subview in memory as a retained object, so that you can remove it.
2)如果要将一个单元格设为你有点提到:
当numberOfSectionsInTableView为零时,返回1.然后,在numberOfRowsInSection中也返回1。
在cellForRowAtIndexPath中,检查numberOfSectionsInTableView和numberOfRowsInSection,如果它们应该为零(毕竟你返回1),则显示任何消息。
2) If you want to make one cell as you somewhat mentioned: When numberOfSectionsInTableView is zero, return 1. Then, in numberOfRowsInSection return 1 as well. In cellForRowAtIndexPath, check both numberOfSectionsInTableView and numberOfRowsInSection, and if they SHOULD be zero (after all you returned 1), then display whatever message.
解决方案1需要维护一个变量来保存子视图,但代码更短更清晰,我相信CPU密集度更低(不会导致任何重大性能问题)。
Solution 1 requires maintaining a variable to hold the subview, but is shorter and cleaner code, and I believe is less CPU intensive (not that either will cause any significant performance issues).
编辑以包含代码:
它实际上与我想的有点不同,但基本相同。
这里显然有些东西需要改变以适应你的代码和品味。
Edit to include code: It was actually a little different than I thought, but basically the same. There are obviously things in here that are to be changed to fit your code and taste.
(确保声明 UIView * nomatchesView; in .h)
(Make sure to declare UIView *nomatchesView; in .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];
}
这篇关于当没有可用于tableView的数据时显示自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!