UIList视图仅在单元格离开视图时显示单元格一次



我一直在学习本教程,我可以显示单元格信息,但前提是该特定单元格不在视图中。例如,底部的三个单元格加载只是找到,因为它们在"折叠"下面,我必须滚动才能找到它们。向下滚动后,会显示顶部单元格。objective-c的新手,所以我甚至不确定从哪里开始。你能给我指个正确的方向吗?

向下滚动后的样子

#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时,您将只返回单元格您之前创建的
    • 当前正在滚动出视图(例如,由于低于表的底部/高于表的顶部而不可见)
  • 因此,前几个出列调用将简单地返回nil,因为没有什么要出列的
  • 现在,您尝试配置不存在的单元格并设置其文本。在目标C中,对nil的调用(消息)不会崩溃,只是什么也不做。不过,这更难调试
  • 最后,检查单元格是否为nil,如果为,则创建一个新单元格并返回此新(未配置)单元格
  • 一旦你滚动,就会有一些单元格被移出队列(因为它们已经被滚动到视图之外),然后这些单元格将被重用和配置,现在你开始看到你的数据

解决方案是重新排列您的代码,并将单元格创建直接放在dequeueReusableCellWithIdentifier:之后

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...

您可以注册单元原型,并使用更新的出列方法来摆脱nil验证。它将始终返回一个已正确调整大小的出队单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionAnimals = [schedule objectForKey:sectionTitle];
NSString *prepschedule = [sectionAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = prepschedule;

//Configure cell
return cell;
}

最新更新