我已经在表格单元格中嵌入了水平表格视图(ala Pulse Reader)。我得到的有趣行为是,它dequeueReusableCellWithIdentifier:
似乎正确地记住了嵌入表视图的偏移量,而我没有做任何事情。但是“记住”有两种口味
第一(预期)
我创建了一个包含 100 个部分和每个部分 1 行的表。每个单元格(每个部分)都有一个嵌入的表格视图,我强制它拥有 100 行和 1 个部分。当我滚动垂直表格视图时,单元格被重用(通过查看单元格的实例名称来检查dequeueReusableCellWithIdentifier:
VerticalTableViewCell *cell = (VerticalTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果我将第一个表格视图单元格的嵌入表格视图向右滚动,例如 10.5 个单元格,当我在垂直表格视图中向下滚动几个单元格时,重用的表格单元格的嵌入表格视图偏移了这 10.5 个单元格。这是有道理的,单元格只是被重用了,我没有重置任何偏移量。假设重用单元格是第 7 行。如果我现在将第 7 行的嵌入表格视图(由于重用,这与第一行的嵌入表格视图相同)滑动到位置 20,当我转到嵌入表格视图的垂直表格视图的顶部时(我有最初移动到 10.5),现在是 20。同样,预期,这些表格单元格是相同的。刚刚重复使用。
第二(需要,但不知道它是如何工作的)
现在我填写我的真实数据(而不仅仅是打印
[cell.textLabel setText:[NSString stringWithFormat: @"here: %d", indexPath.row]];
到细胞。
现在,突然之间,我的嵌入式表格视图确实记得它们在哪里。我再次确保细胞被重复使用(与上述相同的方法)。但是这一次,当我滚动第一个嵌入的表格视图(到位置 10.5)并向下滚动到第七行时,第七行是它的起点。当我将第一行滚动回视图时,它就在我离开它的地方。
这是使用 Xcode 4.2 和 iOS 5.0。有什么聪明的东西dequeueReusableCellWithIdentifier:
吗?
在旧学校(iOS 5.0 之前),我会检查
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
一些逻辑来查看正在发生的事情,但是对于 StoryBoard,这些不再是必需的。
行为不同的 2 个示例项目有点大,但是如果您想查看任何相关代码,请告诉我。
抱歉含糊不清,我只是想准确掌握这些表视图的内存管理发生了什么。
所有连接都通过 IBOutlets 并在 StoryBoard 中进行。
谢谢!
第一个垂直表视图控制器
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlayerCell";
PlayerCell *cell = (PlayerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.games = [NSMutableArray arrayWithCapacity:1];
if(indexPath.section <40){
//[cell.textLabel setText:[NSString stringWithFormat:@"h343: %d", indexPath.section]];
}
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
cell.htv.transform = rotateTable;
cell.htv.frame = CGRectMake(0, 0, 320, 120);
return cell;
}
第一个水平表视图委托/数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
//[cell.myLabel setText:[NSString stringWithFormat:@"%d", indexPath.row]];
[cell.textLabel setText:[NSString stringWithFormat: @"here: %d", indexPath.row]];
return cell;
}
第二个垂直表视图控制器
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.leagues count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"VerticalTableViewCell";
VerticalTableViewCell *cell = (VerticalTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"Cell: %@ for index: %d", cell, indexPath.section);
cell.games = [self.leagues objectAtIndex:indexPath.section];
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
cell.horizontalTableView.transform = rotateTable;
cell.horizontalTableView.frame = CGRectMake(0, 0, 320, 120);
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.section = indexPath.section;
return cell;
}
第二个水平表视图委托/数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.games count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"HorizontalTableViewCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
GameTableCell *cell = (GameTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
//[cell.myLabel setText:[NSString stringWithFormat:@"%d", indexPath.row]];
NSDictionary *game = [self.games objectAtIndex:indexPath.row];
[cell.homeTeamLogo setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[game objectForKey:@"LogoImage_HomeTeam"]]]];
[cell.visitingTeamLogo setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[game objectForKey:@"LogoImage_VisitingTeam"]]]];
[cell.homeTeamName setText:[NSString stringWithFormat:@"%@", [game objectForKey:@"AbbreviatedName_HomeTeam"]]];
[cell.visitingTeamName setText:[NSString stringWithFormat:@"%@", [game objectForKey:@"AbbreviatedName_VisitingTeam"]]];
NSDictionary *gameTime = [game objectForKey:@"GameTime"];
NSString *minutes = [NSString stringWithFormat:@"%@", [gameTime objectForKey:@"Minutes"]];
if([minutes length]==1){
minutes = @"00";
}
NSString *timeOfGame = [NSString stringWithFormat:@"%@:%@",[gameTime objectForKey:@"Hours"], minutes];
[cell.gameTime setText:timeOfGame];
return cell;
}