我一直在this tutorial工作,我可以显示单元格信息,但前提是该特定单元格不在视图范围内。例如,底部的三个单元格加载只是因为它们低于" fold"我必须滚动才能找到他们。向下滚动后,顶部单元格出现。对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
答案 0 :(得分:1)
我认为您将单元格创建代码放在错误的位置:
dequeueReusableCellWithIdentifier
时,您将只返回单元格
nil
,因为没有任何东西可以出列。nil
的调用(消息)不会崩溃,但根本不执行任何操作。但是,调试起来更难。nil
,如果是,则创建一个新单元并返回此新的(未配置的)单元格。解决方案是重新排列您的代码,并在dequeueReusableCellWithIdentifier
之后直接创建单元格:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
答案 1 :(得分:0)
您可以注册单元格原型并使用更新的出列方法来摆脱零验证。它将始终返回正确调整大小的出列单元格。
* {
margin: 0;
padding: 0;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 87.5%;
width: 650px;
margin: 0 auto;
border: 3px solid blue;
padding: 15px 25px;
}
h1 {
font-size: 150%;
}
h2 {
font-size: 120%;
padding: .25em 0 .25em 25px;
cursor: pointer;
background: url(images/plus.png) no-repeat left center;
}
h2.minus {
background: url(images/minus.png) no-repeat left center;
}
a {
color: black;
text-decoration: none;
}
a:focus, a:hover {
color: blue;
}
div {
display: none;
}
div.open {
display: block;
}
ul {
padding-left: 45px;
}
li {
padding-bottom: .25em;
}
p {
padding-bottom: .25em;
padding-left: 25px;
}