テーブル セル内に水平テーブル ビューを埋め込みました (Pulse Reader のように)。私が得ている興味深い動作は、dequeueReusableCellWithIdentifier:
何もしなくても、埋め込まれたテーブル ビューのオフセットを正しく記憶しているように見えることです。しかし、「思い出す」には 2 つの味があります。
最初(予定)
100 個のセクションとセクションごとに 1 行のテーブルを作成します。各セル (各セクション) は、100 行と 1 つのセクションを持つように強制する埋め込みテーブル ビューを取得します。垂直のテーブル ビューをスクロールすると、セルが再利用されます (後でセルのインスタンス名を見て確認します)。dequeueReusableCellWithIdentifier:
VerticalTableViewCell *cell = (VerticalTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
最初のテーブル ビュー セルの埋め込みテーブル ビューを右にスクロールすると (たとえば 10.5 セル)、垂直テーブル ビューでいくつかのセルを下にスクロールすると、再利用されたテーブル セルの埋め込みテーブル ビューが 10.5 セル分オフセットされます。これは理にかなっています。セルは再利用されただけで、オフセットをリセットしませんでした。再利用されたセルが 7 行目だったとします。7 番目の行の埋め込みテーブル ビュー (これは再利用のため、行 1 と同じ埋め込みテーブル ビューです) を位置 20 にスライドすると、テーブル ビューを埋め込んだ垂直テーブル ビュー (私が持っていたもの) の上部に移動します。最初は 10.5 に移動されました)、現在は 20 です。繰り返しになりますが、これらの表のセルは同じです。再利用しただけです。
2番目(望ましいが、それがどのように機能するかわかりません)
ここで、実際のデータを入力します (単に印刷するのではなく)
[cell.textLabel setText:[NSString stringWithFormat: @"here: %d", indexPath.row]];
セルに。
さて、突然、埋め込まれたテーブル ビューがどこにあったか覚えているようになりました。セルが再利用されていることをもう一度確認します (上記と同じ方法)。しかし今回は、最初の埋め込みテーブル ビューを (位置 10.5 まで) スクロールし、7 行目までスクロールすると、7 行目が開始点になります。最初の行をスクロールして表示に戻すと、元の場所に戻ります。
これは 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];
}
何が起こっているかを確認するためのロジックが少しありましたが、StoryBoards ではそれらは不要になりました。
異なる動作をする 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;
}
2 番目の垂直テーブル ビュー コントローラー
- (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;
}
2 番目の水平テーブル ビュー デリゲート/データ ソース
- (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;
}