我一直在努力this tutorial我可以显示单元格信息,但前提是该特定单元格不在 View 中。例如,底部的三个单元格加载只是找到,因为它们位于“折叠”下方,我必须滚动才能找到它们。一旦我向下滚动,顶部的单元格就会出现。对 Objective-C 很陌生,所以我什至不知道从哪里开始。你能指出我正确的方向吗?
What it looks like after scrolling down
#import "agendaController.h"
@implementation agendaController{
NSDictionary *schedule;
NSArray *scheduleSectionTitles;
}
- (IBAction)goBack:(UIStoryboardSegue *)segue{
}
- (void)viewDidLoad {
[super viewDidLoad];
//Will be JSON from web
schedule = @{@"Monday, February 6th" : @[@"6:15 p.m. VIP ticket access",
@"6:30 p.m. Doors open",
@"7:00 p.m. General Session 1"
],
@"Tuesday, February 7th" : @[
@"9:30 a.m. VIP ticket access",
@"9:45 a.m. Doors open",
@"10 a.m. General Session 2",
@"6:15 p.m. VIP ticket access",
@"6:30 p.m. Doors open",
@"7:00 p.m. General Session 3"
],
@"Wednesday, February 8th" : @[
@"9:30 a.m. VIP ticket access",
@"9:45 a.m. Doors open",
@"10 a.m. General Session 4",
@"9:45 a.m. Doors open",
@"9:30 a.m. VIP ticket access",
@"7:00 p.m. General Session 5 (Baptisms immediately following service)"
]
};
scheduleSectionTitles = [[schedule allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [scheduleSectionTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [scheduleSectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:section];
NSArray *sectionSchedule = [schedule objectForKey:sectionTitle];
return [sectionSchedule count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionAnimals = [schedule objectForKey:sectionTitle];
NSString *prepschedule = [sectionAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = prepschedule;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Configure cell
return cell;
}
@end
最佳答案
我认为您将单元格创建代码放在了错误的位置:
- 当您调用
dequeueReusableCellWithIdentifier
时,您只会得到返回的单元格- 您之前创建的和
- 当前正在滚动出 View (例如,因为低于表格底部/高于表格顶部而不可见)。
- 因此,前一些 dequeue 调用将简单地返回
nil
,因为没有任何要 dequeued 的东西。 - 现在您尝试配置不存在 单元格并设置其文本。在 Objective C 中,对
nil
的调用(消息)不会崩溃,而只是什么都不做。不过,这更难调试。 - 最后,您检查单元格是否为
nil
,如果是,则创建一个新单元格并返回这个新的(未配置的)单元格。 - 滚动后,有些单元格将出列(因为它们已滚动到 View 之外),然后这些单元格将被重用和配置,现在您可以开始查看数据。
解决方案是重新排列代码,并将单元格创建直接放在dequeueReusableCellWithIdentifier
之后:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
关于ios - UIList View 仅在单元格不在 View 中时显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41914542/